中介者模式(Mediator Pattern)
概念:
· 一种行为型设计模式;
· 通过一个中介者对象来封装对象之间的交互;
· 各个对象之间不再直接引用或通信,而是通过中介者转发消息或协调行为;
· 核心思想是所有的对象都通过一个中介者进行交互,避免对象之间形成网状的依赖关系;
UML结构:
+----------------+ | IMediator | <<interface>> +----------------+ | +Send(msg, c) | | +Register(c) | +----------------+ ^ | +------------------------+ | ConcreteMediator | +------------------------+ | -colleagues: List<Colleague> | +------------------------+ | +Send(msg, c) | | +Register(c) | +------------------------+ | --------------------------------- | | | +----------------+ +----------------+ +----------------+ | Colleague | | Colleague | | Colleague | +----------------+ +----------------+ +----------------+ | -mediator | | -mediator | | -mediator | | +Send(msg) | | +Send(msg) | | +Send(msg) | | +Receive(msg) | | +Receive(msg) | | +Receive(msg) | +----------------+ +----------------+ +----------------+ ^ ^ ^ | | | +----------------+ +----------------+ +----------------+ | ConcreteUser | | ConcreteUser | | ConcreteUser | +----------------+ +----------------+ +----------------+ | +Receive(msg) | | +Receive(msg) | | +Receive(msg) | +----------------+ +----------------+ +----------------+代码示例:
cs/// <summary> /// 中介者接口 /// </summary> public interface IMediator { void Send(string message, Colleague colleague); } /// <summary> /// 抽象的同事类 /// </summary> public abstract class Colleague { protected IMediator mediator; public string Name { get; private set; } public Colleague(string name, IMediator mediator) { this.Name = name; this.mediator = mediator; } public void Send(string message) { mediator.Send(message, this); } public abstract void Receive(string message); } /// <summary> /// 具体同事类 /// </summary> public class ConcreteColleague : Colleague { public ConcreteColleague(string name, IMediator mediator) : base(name, mediator) { } public override void Receive(string message) { Console.WrileLine($"{Name} 收到消息: {message}"); } } /// <summary> /// 具体中介者 /// </summary> public class ConcreteMediator : IMediator { private List<Colleague> colleagueList = new(); public void Register(Colleague colleague) { if (!colleague.Contains(Colleague)) colleagueList.Add(colleague); } public void Send(string message, Colleague sender) { foreach (var colleague in colleagueList) { if (colleague != sender) colleague.Receive($"{sender.Name} 说: {message}"); } } } /// <summary> /// 客户端 /// </summary> public class Client { public static void Main() { ConcreteMediator mediator = new ConcreteMediator(); Colleague alice = new ConcreteColleague("Alice", mediator); Colleague bob = new ConcreteColleague("Bob", mediator); Colleague charlie = new ConcreteColleague("Charlie", mediator); mediator.Register(alice); mediator.Register(bob); mediator.Register(charlie); alice.Send("大家好,我是Alice"); bob.Send("你好 Alice,我是Bob"); charlie.Send("你们好,我是Charlie"); } }特点:
优点:· 降低耦合:同事类之间不依赖其他类,只依赖中介者;
· 系统中的对象复杂交互都在中介者中,这便于统一管理;
· 易于拓展和维护:添加新同事或修改逻辑时只需要修改中介者即可 ;
缺点:· 中介者的存在增加了系统的复杂度;
· 中介者又需要维护同事,又需要处理业务逻辑,违背了单一职责原则 ;
适用场景:
· 对象之间通信复杂且紧密的场景 ;
· 希望通过集中管理减少类之间的耦合;
· 希望提供系统的可维护性和可拓展性 ;
举例场景:
· 聊天室系统:用户之间不直接发送消息,而是通过中介者进行转发;
· 飞机调度系统:每架飞机不直接和其他飞机通信,而是通过 空中交通管制中心协调起降;
· GUI 界面控件:窗口或对话框类 进行交互,而不是直接相互调用事件处理;
【设计模式】中介者模式
大飞pkz2025-09-29 9:00
相关推荐
青禾网络1 天前
Web 前端如何接入 AI 音效生成:从零到可用的完整方案ZJPRENO2 天前
吃透软件开发六大设计原则,告别烂代码咖啡八杯2 天前
GoF设计模式——命令模式花椒技术3 天前
HJPusher / HJPlayer SDK 实践:我们为什么把直播推播链路拆成一套可复用能力雨落倾城夏未凉3 天前
第四章c#方法-参数数组和可选参数(16)艺艺生辉3 天前
迭代器模式-"我也想被增强for循环"唐青枫4 天前
线程不是越多越快:C#.NET Thread 生命周期、同步与后台工作线程实战咖啡八杯4 天前
GoF设计模式——策略模式唐青枫5 天前
别只会反射:C#.NET Emit 动态生成代码实战详解