【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();
     }
 }

运行结果

相关推荐
勘察加熊人29 分钟前
forms实现连连看
c#
hvinsion29 分钟前
PPT助手:一款集计时、远程控制与多屏切换于一身的PPT辅助工具
c#·powerpoint·ppt·ppt助手·ppt翻页
weixin_307779132 小时前
使用C#实现从Hive的CREATE TABLE语句中提取分区字段名和数据类型
开发语言·数据仓库·hive·c#
时光追逐者3 小时前
在 Blazor 中使用 Chart.js 快速创建数据可视化图表
开发语言·javascript·信息可视化·c#·.net·blazor
高 朗4 小时前
2025高频面试设计模型总结篇
设计模式·面试·职场和发展
与火星的孩子对话4 小时前
Unity3D开发AI桌面精灵/宠物系列 【三】 语音识别 ASR 技术、语音转文本多平台 - 支持科大讯飞、百度等 C# 开发
人工智能·unity·c#·游戏引擎·语音识别·宠物
response_L4 小时前
国产系统统信uos和麒麟v10在线打开word给表格赋值
java·c#·word·信创·在线编辑
MasterNeverDown4 小时前
Swagger2Md:让WebAPI文档生成变得轻松高效
c#
向宇it4 小时前
【零基础入门unity游戏开发——2D篇】2D 游戏场景地形编辑器——TileMap的使用介绍
开发语言·游戏·unity·c#·编辑器·游戏引擎
闪电麦坤9513 小时前
C#:base 关键字
开发语言·c#