学习而来,代码是自己敲的。也有些自己的理解在里边,有问题希望大家指出。
中介者模式(Mediator Pattern),用一个中介对象来封装一系列的对象交互,中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。当某些对象之间的作用发生改变时,不会立即影响其他的一些对象之间的作用。保证这些作用可以彼此独立的变化。中介者模式将多对多的相互作用转化为一对多的相互作用。中介者模式将对象的行为和协作抽象化,把对象在小尺度的行为上与其他对象的相互作用分开处理。
只有一个仲裁者.
- 降低了类的复杂度,将一对多转化成了一对一。
- 各个类之间的解耦。
- 符合迪米特法则。
- 符合单一职责原则
- 符合开闭原则:可以在不更改现有代码的情况下添加新的中介程序
- 如果用不好,那中介者类会变庞大,变得复杂难以维护。
using DesignPattern.MediatorPattern;
using System;namespace DesignPattern
{internal class Program{static void Main(string[] args){MediatorHelper();}#region Pattern - Mediatorstatic void MediatorHelper(){ConcreteMediator m = new ConcreteMediator();//让 具体同事类 认识 中介者对象 ConcreteColleague c1 = new ConcreteColleague("c1", m);ConcreteColleague c2 = new ConcreteColleague("c2", m);ConcreteColleague c3 = new ConcreteColleague("c3", m);ConcreteColleague c4 = new ConcreteColleague("c4", m);//让 中介者 认识各个 具体同事类对象m.AddColleague(c1);m.AddColleague(c1);m.AddColleague(c2);m.AddColleague(c3);m.AddColleague(c4);c1.Send("c1: 大家加班到这么晚,都吃过了吗?");Console.WriteLine();c2.Send("c2: 没有呢,c1你打算请客不?");Console.WriteLine();c3.Send("c3: 什么? c1打算请客么?");Console.WriteLine();c4.Send("c4: 大家AA吧,挣钱都不容易的.");Console.WriteLine();c1.Send("c1: 还是c4好,去吃饭吧。");Console.ReadLine();}#endregion}
}//======================================================================================namespace DesignPattern.MediatorPattern
{/// /// 抽象中介者/// public abstract class Mediator{/// /// 发送消息/// /// 消息内容/// 给谁发public abstract void Send(string message, Colleague colleague);}/// /// 抽象同事类/// public abstract class Colleague{//中介者protected Mediator m_mediator;protected string m_name;public Colleague(Mediator mediator, string name){this.m_mediator = mediator;m_name = name;}public string Name => m_name;/// /// 发送消息/// /// 消息内容public abstract void Send(string message);/// /// 接受消息/// /// 消息内容public abstract void Notify(string message);}//======================================================================================/// /// 具体的中介者, eg: ChangedMediator/// public class ConcreteMediator : Mediator{private List colleagues;public ConcreteMediator(){colleagues = new List();}~ConcreteMediator(){while (colleagues.Count > 0){colleagues.RemoveAt(0);}colleagues.Clear();colleagues = null;}public void AddColleague(Colleague colleague){if (colleagues.Contains(colleague)){Console.WriteLine($"中介者已经知道当前同事{colleague.Name}了,不要重复介绍.");return;}colleagues.Add(colleague);}public override void Send(string message, Colleague colleague){for (int i = 0; i < colleagues.Count; i++){if (colleagues[i] == colleague){//自己不能给自己发送消息continue;}colleagues[i].Notify(message);}}}//======================================================================================/// /// 具体同事类/// public class ConcreteColleague : Colleague{public ConcreteColleague(string name, Mediator mediator) : base(mediator, name){}public override void Send(string message){m_mediator.Send(message, this);}public override void Notify(string message){Console.WriteLine($"同事 {m_name} 得到信息:{message}");}}
}
个人感觉像是自如平台、调度中心这样的,还有一个MVC框架,我觉得这个C层就是一个中介者,但MVC还算不上是中介者模式,如果事件也需要让注册者知道注册的是哪一个事件,那事件也是一个很好的中介者模式。不过都是自己的拙见,如果文章公开后,内容有误,还希望指出。
今日分享结束啦,小伙伴们你们get到了么,你们有没有更好的办法呢,可以评论区留言分享,也可以加我QQ:841298494,大家一起进步。
- 博客杂货铺
- GoF23 种设计模式的分类和功能
上一篇:逻辑回归全面解析