详解设计模式中的中介者模式在C++编程中的运用
作用:用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。 结构图如下: Colleage抽象同事类,而ConcreteColleage是具体同时类,每个具体同事只知道自己的行为,而不了解其他同事类的情况,但它们却都认识中介者对象,Mediator是抽象中介者,定义了同事对象到中介者对象的接口,ConcreteMediator是具体中介者对象,实现抽象类的方法,它需要知道所有具体同事类,并从具体同事接受消息,向具体同事对象发出命令。 Colleage类,抽象同事类 Mediator,抽象中介者类 说明: 1. Mediator 模式中,每个Colleague 维护一个 Mediator,当要进行通信时,每个具体的 Colleague 直接向ConcreteMediator 发信息,至于信息发到哪里,则由 ConcreteMediator 来决定。 2. ConcreteColleagueA 和 ConcreteColleagueB 不必维护对各自的引用,甚至它们也不知道各个的存在。 3. 优点是,各个 Colleague 减少了耦合。 4. 缺点是,由于 Mediator 控制了集中化,于是就把 Colleague 之间的交互复杂性变为了中介者的复杂性,也就是中介者会变的比任何一个 Colleague 都复杂。 中介者模式很容易在系统中应用,也很容易在系统中误用。当系统中出现了“多对多”交互复杂的对象群时,不要急于使用中介者模式,而要先反思你的系统在设计上是不是合理。 Mediator的出现减少了各个Colleage的耦合,使得可以独立地改变和复用各个Colleage类和Mediator; 由于ConcreteMediator控制了集中化,于是就把交互复杂性变为了中介者的复杂性,这使得中介者会变得比任何一个ConcreteColleage都复杂。 中介者模式的优点来自集中控制,其缺点也是它。 中介者模式一般应用于一组对象以定义良好但是复杂的方式进行通信的场合。 很好的例子:聊天室: // Mediator pattern -- Real World example using System; using System.Collections; namespace DoFactory.GangOfFour.Mediator.RealWorld { // MainApp test application class MainApp { static void Main() { // Create chatroom Chatroom chatroom = new Chatroom(); // Create participants and register them Participant George = new Beatle("George"); Participant Paul = new Beatle("Paul"); Participant Ringo = new Beatle("Ringo"); Participant John = new Beatle("John") ; Participant Yoko = new NonBeatle("Yoko"); chatroom.Register(George); chatroom.Register(Paul); chatroom.Register(Ringo); chatroom.Register(John); chatroom.Register(Yoko); // Chatting participants Yoko.Send ("John","Hi John!"); Paul.Send ("Ringo","All you need is love"); Ringo.Send("George","My sweet Lord"); Paul.Send ("John","Can't buy me love"); John.Send ("Yoko","My sweet love") ; // Wait for user Console.Read(); } } // "Mediator" abstract class AbstractChatroom { public abstract void Register(Participant participant); public abstract void Send( string from,string to,string message); } // "ConcreteMediator" class Chatroom : AbstractChatroom { private Hashtable participants = new Hashtable(); public override void Register(Participant participant) { if (participants[participant.Name] == null) { participants[participant.Name] = participant; } participant.Chatroom = this; } public override void Send( string from,string message) { Participant pto = (Participant)participants[to]; if (pto != null) { pto.Receive(from,message); } } } // "AbstractColleague" class Participant { private Chatroom chatroom; private string name; // Constructor public Participant(string name) { this.name = name; } // Properties public string Name { get{ return name; } } public Chatroom Chatroom { set{ chatroom = value; } get{ return chatroom; } } public void Send(string to,string message) { chatroom.Send(name,to,message); } public virtual void Receive( string from,string message) { Console.WriteLine("{0} to {1}: '{2}'",from,Name,message); } } //" ConcreteColleague1" class Beatle : Participant { // Constructor public Beatle(string name) : base(name) { } public override void Receive(string from,string message) { Console.Write("To a Beatle: "); base.Receive(from,message); } } //" ConcreteColleague2" class NonBeatle : Participant { // Constructor public NonBeatle(string name) : base(name) { } public override void Receive(string from,string message) { Console.Write("To a non-Beatle: "); base.Receive(from,message); } } } 适用场景:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |