重要概念
1. 中介者模式,消息的传递需要在中介者对象中进行判断控制该发送给谁,中介者对象还需要知道所有的传递者对象,关键就在这中介者对象上,他的职责太多了。
2. 优点:中介者减少了各个沟通者的耦合,使得可以独立地改变和复用各个沟通者类和中介者。
3. 优点:中介者把如何协作进行了抽象,将中介概念封装在对象中,将关注从对象本身转移到了他们的交互上来,站在更宏观的角度去看待系统,也就是集中控制。
4. 缺点:由于中介者对象的控制集中化,于是就把交互复杂性变成了中介者的复杂性,这就使得中介者会变得比任何一个传递者都要复杂,也就是中介者太复杂。
5. 中介者模式一般应用于一组对象以定义良好但是复杂的方式进行通信的场合,以及想定制一个分布在多个类中的行为,而又不想生成太多的子类的场合。
6.
示例代码
using System; using System.Collections.Generic; using System.Text; namespace 中介者模式 { class Program { static void Main(string[] args) { ConcreteMediator m = new ConcreteMediator(); ConcreteColleague1 c1 = new ConcreteColleague1(m); ConcreteColleague2 c2 = new ConcreteColleague2(m); m.Colleague1 = c1; m.Colleague2 = c2; c1.Send("吃过饭了吗?"); c2.Send("没有呢,你打算请客?"); Console.Read(); } } abstract class Mediator { public abstract void Send(string message, Colleague colleague); } class ConcreteMediator : Mediator { private ConcreteColleague1 colleague1; private ConcreteColleague2 colleague2; public ConcreteColleague1 Colleague1 { set { colleague1 = value; } } public ConcreteColleague2 Colleague2 { set { colleague2 = value; } } public override void Send(string message, Colleague colleague) { if (colleague == colleague1) { colleague2.Notify(message); } else { colleague1.Notify(message); } } } abstract class Colleague { protected Mediator mediator; public Colleague(Mediator mediator) { this.mediator = mediator; } } class ConcreteColleague1 : Colleague { public ConcreteColleague1(Mediator mediator) : base(mediator) { } public void Send(string message) { mediator.Send(message, this); } public void Notify(string message) { Console.WriteLine("同事1得到信息:" + message); } } class ConcreteColleague2 : Colleague { public ConcreteColleague2(Mediator mediator) : base(mediator) { } public void Send(string message) { mediator.Send(message, this); } public void Notify(string message) { Console.WriteLine("同事2得到信息:" + message); } } }
安理会例题
using System; using System.Collections.Generic; using System.Text; namespace 中介者模式 { class Program { static void Main(string[] args) { UnitedNationsSecurityCouncil UNSC = new UnitedNationsSecurityCouncil(); USA c1 = new USA(UNSC); Iraq c2 = new Iraq(UNSC); UNSC.Colleague1 = c1; UNSC.Colleague2 = c2; c1.Declare("不准研制核武器,否则要发动战争!"); c2.Declare("我们没有核武器,也不怕侵略。"); Console.Read(); } } //联合国机构 abstract class UnitedNations { /// <summary> /// 声明 /// </summary> /// <param name="message">声明信息</param> /// <param name="colleague">声明国家</param> public abstract void Declare(string message, Country colleague); } //联合国安全理事会 class UnitedNationsSecurityCouncil : UnitedNations { private USA colleague1; private Iraq colleague2; public USA Colleague1 { set { colleague1 = value; } } public Iraq Colleague2 { set { colleague2 = value; } } public override void Declare(string message, Country colleague) { if (colleague == colleague1) { colleague2.GetMessage(message); } else { colleague1.GetMessage(message); } } } //国家 abstract class Country { protected UnitedNations mediator; public Country(UnitedNations mediator) { this.mediator = mediator; } } //美国 class USA : Country { public USA(UnitedNations mediator) : base(mediator) { } //声明 public void Declare(string message) { mediator.Declare(message, this); } //获得消息 public void GetMessage(string message) { Console.WriteLine("美国获得对方信息:" + message); } } //伊拉克 class Iraq : Country { public Iraq(UnitedNations mediator) : base(mediator) { } //声明 public void Declare(string message) { mediator.Declare(message, this); } //获得消息 public void GetMessage(string message) { Console.WriteLine("伊拉克获得对方信息:" + message); } } }
冯瑞涛