国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

詳解設計模式中的中介者模式在C++編程中的運用

2020-05-23 14:06:42
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了設計模式中的中介者模式在C++編程中的運用,中介者模式將對象間的通信封裝到一個類中,將多對多的通信轉化為一對多的通信,降低了系統的復雜性,需要的朋友可以參考下
 

作用:用一個中介對象來封裝一系列的對象交互。中介者使各對象不需要顯式地相互引用,從而使其耦合松散,而且可以獨立地改變它們之間的交互。

結構圖如下:

設計模式,中介者模式,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"  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);  } }}

適用場景:

  • 一組對象以定義良好但是復雜的方式進行通信。產生的相互依賴關系結構混亂且難以理解。
  • 一個對象引用其他很多對象并且直接與這些對象通信,導致難以復用該對象。
  • 想定制一個分布在多個類中的行為,而又不想生成太多的子類。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 读书| 根河市| 达州市| 喜德县| 赞皇县| 新干县| 桃园县| 澄城县| 桐梓县| 康平县| 正宁县| 南宁市| 垣曲县| 固阳县| 霍邱县| 汝南县| 开封县| 从江县| 雅江县| 弥勒县| 武川县| 迭部县| 景谷| 东台市| 新和县| 光山县| 文水县| 长岭县| 修文县| 盈江县| 达日县| 罗定市| 汉川市| 扎兰屯市| 同江市| 离岛区| 阜城县| 明溪县| 大方县| 陇南市| 水富县|