您的位置:首页 > 财经 > 金融 > 推荐6个免费国外自媒体平台_免费可以看污app秋葵_seo优化排名公司_seo的优点

推荐6个免费国外自媒体平台_免费可以看污app秋葵_seo优化排名公司_seo的优点

2024/12/23 3:57:22 来源:https://blog.csdn.net/weixin_43626218/article/details/144310691  浏览:    关键词:推荐6个免费国外自媒体平台_免费可以看污app秋葵_seo优化排名公司_seo的优点
推荐6个免费国外自媒体平台_免费可以看污app秋葵_seo优化排名公司_seo的优点

前言

中介者模式:是两者之间通过第三者来帮助传话。

代码

 //抽象接收者public abstract class Receiver{protected Mediator mediator;protected Receiver(Mediator mediator){this.mediator = mediator;}public abstract void SendMessage(string message);public abstract void ReceiveMessage(string message);}//具体接收者Apublic class ReceiverA : Receiver{public ReceiverA(Mediator mediator) : base(mediator){}public override void ReceiveMessage(string message){Console.WriteLine($"{this.GetType().Name} got a message[{message}]");}public override void SendMessage(string message){Console.WriteLine($"{this.GetType().Name}:a message was send...");mediator.SendMessage(this,message);}}//具体接收者Bpublic class ReceiverB : Receiver{public ReceiverB(Mediator mediator) : base(mediator) { }public override void ReceiveMessage(string message){Console.WriteLine($"{this.GetType().Name} got a message[{message}]");}public override void SendMessage(string message){Console.WriteLine($"{this.GetType().Name}:a message was send...");mediator.SendMessage(this, message);}}//中介接口public interface Mediator{void SendMessage(Receiver obj, string message);}//具体中介者类public class ReceiveMediator : Mediator{private Receiver receiverA;private Receiver receiverB;public void SetReceiveA(Receiver receiverA){this.receiverA = receiverA;}public void SetReceiveB(Receiver receiverB){this.receiverB = receiverB;}public void SendMessage(Receiver receiver, string message){if(receiver == receiverA){receiverB.ReceiveMessage(message);}else if(receiver == receiverB){receiverA.ReceiveMessage(message);}}}/** 行为型模式:Behavioral Pattern* 中介者模式:Mediator Pattern*/internal class Program{static void Main(string[] args){ReceiveMediator receiveMediator = new ReceiveMediator();ReceiverA receiverA = new ReceiverA(receiveMediator);ReceiverB receiverB = new ReceiverB(receiveMediator);receiveMediator.SetReceiveA(receiverA);receiveMediator.SetReceiveB(receiverB);receiveMediator.SendMessage(receiverA, "Hello receiverB");receiveMediator.SendMessage(receiverB, "Hello receiverA");Console.ReadLine();}}

运行结果

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com