【C#设计模式(18)——中介者模式(Mediator Pattern)】

前言

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

代码

csharp 复制代码
 //抽象接收者
 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);
 }
 //具体接收者A
 public 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);
     }
 }
 //具体接收者B
 public 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();
     }
 }

运行结果

相关推荐
深度混淆2 小时前
C#,入门教程(04)——Visual Studio 2022 数据编程实例:随机数与组合
开发语言·c#
chance_663 小时前
C# ASP.NET MVC项目内使用ApiController
c#
钢铁男儿8 小时前
C# 委托和事件(事件)
开发语言·c#
喜-喜8 小时前
C# HTTP/HTTPS 请求测试小工具
开发语言·http·c#
等一场春雨9 小时前
Java设计模式 九 桥接模式 (Bridge Pattern)
java·设计模式·桥接模式
susan花雨10 小时前
winfrom项目,引用EPPlus.dll实现将DataTable 中的数据保存到Excel文件
c#
墨笺染尘缘11 小时前
Unity——鼠标是否在某个圆形Image范围内
unity·c#·游戏引擎
等一场春雨12 小时前
Java设计模式 十四 行为型模式 (Behavioral Patterns)
java·开发语言·设计模式
code_shenbing15 小时前
C# 操作 文件
开发语言·c#
小王子102415 小时前
设计模式Python版 单例模式
python·单例模式·设计模式