现在的位置: 首页 > 综合 > 正文

设计模式【9】:中介者模式【对象去耦】

2014年09月05日 ⁄ 综合 ⁄ 共 6305字 ⁄ 字号 评论关闭

  中介者模式(Mediator),用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式的相互引用,从而使其耦合松散,而且可以独立的改变它们之间的交互。

中介者模式UML类图:

       由上图可以看出:Mediator 抽象中介者类,定义了同事对象到中介者对象的接口;Colleague抽象同事类;ConcreteMediator具体中介者类,实现抽象类的方法,它需要知道所有的具体同事类,并从同事接收消息,向具体同事对象发出命令;具体同事类,每个具体同事对象只知道自己的行为,而不了解其它同事类的情况,但它们却都认识中介者对象。

 

中介者模式实现:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace Mediator  
  7. {  
  8.     /*Mediator 抽象中介者类,定义了同事对象到中介者对象的接口。*/  
  9.     abstract class Mediator  
  10.     {  
  11.         public abstract void Send(string message,Colleague colleague);  
  12.     }  
  13.   
  14.     /*Colleague抽象同事类*/  
  15.     abstract class Colleague  
  16.     {  
  17.         protected Mediator mediator;  
  18.   
  19.         public Colleague(Mediator mediator)  
  20.         {  
  21.             this.mediator = mediator;  
  22.         }  
  23.     }  
  24.   
  25.     /*具体中介者类,实现抽象类的方法,它需要知道所有的具体同事类,并从同事接收消息,向具体同事对象发出命令*/  
  26.     class ConcreteMediator : Mediator  
  27.     {  
  28.         /*中介者对象需要了解所有的同事*/  
  29.         private ConcreteClooeague1 colleague1;  
  30.         private ConcreteClooeague2 colleague2;  
  31.   
  32.         public ConcreteClooeague1 Colleague1  
  33.         {  
  34.             set { this.colleague1 = value; }  
  35.         }  
  36.   
  37.         public ConcreteClooeague2 Colleague2  
  38.         {  
  39.             set { this.colleague2 = value; }  
  40.         }  
  41.   
  42.         /*重写发送信息的方法,根据对象来做出选择判断,通知对象。*/  
  43.         public override void Send(string message, Colleague colleague)  
  44.         {  
  45.             if (colleague == colleague1)  
  46.             {  
  47.                 colleague2.Notify(message);  
  48.             }  
  49.             else  
  50.             {  
  51.                 colleague1.Notify(message);  
  52.             }  
  53.         }  
  54.     }  
  55.   
  56.     /*具体同事类,每个具体同事对象只知道自己的行为,而不了解其它同事类的情况,但它们却都认识中介者对象。*/  
  57.     class ConcreteClooeague1 : Colleague  
  58.     {  
  59.         public ConcreteClooeague1(Mediator mediator)  
  60.             : base(mediator)  
  61.         { }  
  62.   
  63.         public void Send(string message)  
  64.         {  
  65.             this.mediator.Send(message,this);  
  66.         }  
  67.   
  68.         public void Notify(string message)  
  69.         {  
  70.             Console.WriteLine("同事1 得到信息:"+message);  
  71.         }  
  72.     }  
  73.   
  74.     class ConcreteClooeague2 : Colleague  
  75.     {  
  76.         public ConcreteClooeague2(Mediator mediator)  
  77.             : base(mediator)  
  78.         { }  
  79.   
  80.         public void Send(string message)  
  81.         {  
  82.             this.mediator.Send(message,this);  
  83.         }  
  84.   
  85.         public void Notify(string message)  
  86.         {  
  87.             Console.WriteLine("同事2 得到信息:" + message);  
  88.         }  
  89.     }  
  90. }  

客户端:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace Mediator  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             ConcreteMediator cm = new ConcreteMediator();  
  13.   
  14.             /*让两个具体同事对象认识同一个中介者*/  
  15.             ConcreteClooeague1 c1 = new ConcreteClooeague1(cm);  
  16.             ConcreteClooeague2 c2 = new ConcreteClooeague2(cm);  
  17.   
  18.             /*让中介者对象认识所有的同事对象*/  
  19.             cm.Colleague1 = c1;  
  20.             cm.Colleague2 = c2;  
  21.   
  22.             /*具体同事类对象的发送信息都是通过中介者来转发*/  
  23.             c1.Send("吃饭了没?");  
  24.             c2.Send("还没,要请客吗?");  
  25.   
  26.             Console.Read();  
  27.         }  
  28.     }  
  29. }  

中介者模式总结:

       对于中介者模式,该设计模式很容易在系统中应用,也很容易在系统中误用。当系统出现了‘多对多’交互复杂的对象群时,不要急于使用中介者模式,而要先反思你的系统在设计上是不是合理。

       中介者模式的使用有很多优点,首先,Mediator的出现减少了各个Colleague的耦合,使得可以独立的改变和复用各个Colleague类和Mediator;

       其次,由于把对象如何协作进行了抽象,将中介作为一个独立的概念并将其封装在一个对象中,这样关注的对象就从对象各自本身的行为转移到了它们之间的交互上来,也就是站在一个更宏观的角度去看待系统。

       然而,由于ConcreteMediator控制了集中化,于是就把交互复杂性变成了中介者的复杂性,这就使得中介者会变得比任何一个ConcreteColleague更复杂。

 

中介者模式案例—安理会:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace MediatorExample  
  7. {  
  8.     /*联合国机构作为中介者*/  
  9.     abstract class UnitedNations  
  10.     {  
  11.         public abstract void Send(string message, Country country);  
  12.     }  
  13.   
  14.     /*Country作为抽象同事类*/  
  15.     abstract class Country  
  16.     {  
  17.         protected UnitedNations un;  
  18.   
  19.         public Country(UnitedNations un)  
  20.         {  
  21.             this.un = un;  
  22.         }  
  23.     }  
  24.   
  25.     /*具体中介者类-联合国安全理事会,实现抽象类的方法,它需要知道所有的具体同事类,并从同事接收消息,向具体同事对象发出命令*/  
  26.     class UnitedNationSecurityCouncil : UnitedNations  
  27.     {  
  28.         /*中介者对象需要了解所有的同事*/  
  29.         private USA colleague1;  
  30.         private Iraq colleague2;  
  31.   
  32.         public USA Colleague1  
  33.         {  
  34.             set { this.colleague1 = value; }  
  35.         }  
  36.   
  37.         public Iraq Colleague2  
  38.         {  
  39.             set { this.colleague2 = value; }  
  40.         }  
  41.   
  42.         /*重写发送信息的方法,根据对象来做出选择判断,通知对象。*/  
  43.         public override void Send(string message, Country country)  
  44.         {  
  45.             if (country == colleague1)  
  46.             {  
  47.                 colleague2.Notify(message);  
  48.             }  
  49.             else  
  50.             {  
  51.                 colleague1.Notify(message);  
  52.             }  
  53.         }  
  54.     }  
  55.   
  56.     /*具体同事类-美国,每个具体同事对象只知道自己的行为,而不了解其它同事类的情况,但它们却都认识中介者对象。*/  
  57.     class USA : Country  
  58.     {  
  59.         public USA(UnitedNations un)  
  60.             : base(un)  
  61.         { }  
  62.   
  63.         public void Send(string message)  
  64.         {  
  65.             this.un.Send(message, this);  
  66.         }  
  67.   
  68.         public void Notify(string message)  
  69.         {  
  70.             Console.WriteLine("美国 得到信息:" + message);  
  71.         }  
  72.     }  
  73.   
  74.     /*伊拉克*/  
  75.     class Iraq : Country  
  76.     {  
  77.         public Iraq(UnitedNations un)  
  78.             : base(un)  
  79.         { }  
  80.   
  81.         public void Send(string message)  
  82.         {  
  83.             this.un.Send(message, this);  
  84.         }  
  85.   
  86.         public void Notify(string message)  
  87.         {  
  88.             Console.WriteLine("伊拉克 得到信息:" + message);  
  89.         }  
  90.     }  
  91.   
  92. }  

客户端:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace MediatorExample  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             UnitedNationSecurityCouncil un = new UnitedNationSecurityCouncil();  
  13.   
  14.             /*让两个具体同事对象认识同一个中介者*/  
  15.             USA usa = new USA(un);  
  16.             Iraq iraq = new Iraq(un);  
  17.   
  18.             /*让中介者对象认识所有的同事对象*/  
  19.             un.Colleague1 = usa;  
  20.             un.Colleague2 = iraq;  
  21.   
  22.             /*具体同事类对象的发送信息都是通过中介者来转发*/  
  23.             usa.Send("不准研制核武器,否则要发动战争。");  
  24.             iraq.Send("我们没有核武器,也不怕侵略。");  
  25.   
  26.             Console.Read();  
  27.         }  
  28.     }  
  29. }  

运行结果:

抱歉!评论已关闭.