您的位置:首页 > 游戏 > 游戏 > 网络文化经营许可证经营范围_燕郊seo_关键词优化流程_个人网站免费制作平台

网络文化经营许可证经营范围_燕郊seo_关键词优化流程_个人网站免费制作平台

2024/12/22 9:24:12 来源:https://blog.csdn.net/weixin_43626218/article/details/144310691  浏览:    关键词:网络文化经营许可证经营范围_燕郊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