• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

详解设计模式中的中介者模式在C++编程中的运用

c++ 搞代码 4年前 (2022-01-06) 28次浏览 已收录 0个评论

这篇文章主要介绍了设计模式中的中介者模式在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 to, string message) { Participant pto = (Participant)participants[to]; if (pto != null) { pto.Receive(from, message); } } } // "AbstractColleague<a style="color:transparent">来源gao($daima.com搞@代@#码(网</a>" 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); } } } 

适用场景:

  • 一组对象以定义良好但是复杂的方式进行通信。产生的相互依赖关系结构混乱且难以理解。
  • 一个对象引用其他很多对象并且直接与这些对象通信,导致难以复用该对象。
  • 想定制一个分布在多个类中的行为,而又不想生成太多的子类。

以上就是详解设计模式中的中介者模式在C++编程中的运用的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:详解设计模式中的中介者模式在C++编程中的运用

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址