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

2020-01-06 14:52:31王振洲

由于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);
  }
 }
}