且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

极速理解设计模式系列:15.中介者模式(Mediator Pattern)

更新时间:2022-10-04 20:01:00

五个角色:抽象中介者(Mediator)、具体中介者(ConcreteMediator)、同事类(Colleague)、具体同事类(ConcreteColleague)、客户端(Client) 

        抽象中介者(Mediator):定义接口让同事对象之间通讯

        具体中介者(ConcreteMediator):实现接口,并且协调维护同事对象

        同事类(Colleague):其内部引用中介者对象,并且提供一个接口发送消息

        具体同事类(ConcreteColleague):实现接口,向中介者发送消息

        客户端(Client) :让具体同事类认识中介者,并且让中介者认识同事,然后同事发送消息即可。

 实现思路:客户端让中介者和同事互相认识,然后同事向中介者发送消息,中介者将消息转发给另外的需要接收消息的同事。

 类图:

极速理解设计模式系列:15.中介者模式(Mediator Pattern)

应用场景:一个用户使用中国移动的电话向另外一个用户发消息。

分析:用户发消息给另外一个用户的时候,实际上用户是发送消息给中国移动,然后中国移动信息控制器发送消息给另外一个用户,这里中介者是中国移动。

        下面我们在控制台程序去演示一下如何使用Mediator Pattern:

        一、抽象中介者(Mediator)

 


  1. //抽象中介者(Mediator) 
  2. abstract class ChinaMobile 
  3.     public abstract void Phone(string talkMessage, User user); 

        二、具体中介者(ConcreteMediator)

 


  1. //具体中介者(ConcreteMediator) 
  2. class ConcreteChinaMobile:ChinaMobile 
  3.     public User1 User1 { get; set; } 
  4.     public User2 User2 { get; set; } 
  5.     public override void Phone(string talkMessage, User user
  6.     { 
  7.         if (user == User1) 
  8.         { 
  9.             User2.GetMessage(talkMessage); 
  10.         } 
  11.         else 
  12.         { 
  13.             User1.GetMessage(talkMessage); 
  14.         } 
  15.     } 

        三、同事类(Colleague)

 


  1. //同事类(Colleague) 
  2. abstract class User 
  3.     protected ChinaMobile chinaMobile; 
  4.     public User(ChinaMobile mobile) 
  5.     { 
  6.         chinaMobile = mobile; 
  7.     } 

        四、具体同事类(ConcreteColleague)

 


  1. //具体同事类(ConcreteColleague) 
  2. class User1 : User 
  3.     public User1(ChinaMobile chinaMobile) : base(chinaMobile) { } 
  4.     public void Talk(string talkMessage) 
  5.     { 
  6.         chinaMobile.Phone(talkMessage, this); 
  7.     } 
  8.     public void GetMessage(string talkMessage) 
  9.     { 
  10.         Console.WriteLine("用户1得到消息:"+talkMessage); 
  11.     } 
  12.  
  13. //具体同事类(ConcreteColleague) 
  14. class User2 : User 
  15.     public User2(ChinaMobile chinaMobile) : base(chinaMobile) { } 
  16.     public void Talk(string talkMessage) 
  17.     { 
  18.         chinaMobile.Phone(talkMessage, this); 
  19.     } 
  20.     public void GetMessage(string talkMessage) 
  21.     { 
  22.         Console.WriteLine("用户2得到消息:" + talkMessage); 
  23.     } 

        五、客户端(Client) 

 


  1. //客户端(Client) 
  2. class Program 
  3.     static void Main(string[] args) 
  4.     { 
  5.  
  6.         ///让两个具体同事类认识中介者        
  7.         ConcreteChinaMobile cm = new ConcreteChinaMobile(); 
  8.  
  9.         ///让中介者认识具体对象          
  10.         User1 user1 = new User1(cm); 
  11.         User2 user2 = new User2(cm); 
  12.         cm.User1 = user1; 
  13.         cm.User2 = user2; 
  14.         Console.WriteLine("用户1给用户2发消息"); 
  15.         user1.Talk("你在哪儿?"); 
  16.         Console.WriteLine("用户2给用户1发消息"); 
  17.         user2.Talk("我在成都."); 
  18.  
  19.         Console.Read(); 
  20.     } 

        如需源码请点击 MediatorPattern.rar 下载。


本文转自程兴亮 51CTO博客,原文链接:http://blog.51cto.com/chengxingliang/827019