Terry's blog

Focus on bigdata and cloud.

博客园 首页 新随笔 联系 订阅 管理
依赖关系的转化:        

 


动机(Motivate):
     在软件构建过程中,经常会出现多个对象互相关联交互的情况,对象之间常常会维持一种复杂的引用关系,如果遇到一些需求的更改,这种直接的引用关系将面临不断的变化。
    在这种情况下,我们可使用一个“中介对象”来管理对象间的关联关系,避免相互交互的对象之间的紧耦合引用关系,从而更好地抵御变化。

意图(Intent):
    用一个中介对象来封装一系列对象交互。中介者使各对象不需要相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。                                                                          ------《设计模式》GOF
结构图(Struct):
         
适用性:
    1.一组对象以定义良好但是复杂的方式进行通信。产生的相互依赖关系结构混乱且难以理解。
    2.一个对象引用其他很多对象并且直接与这些对象通信,导致难以复用该对象。
    3.想定制一个分布在多个类中的行为,而又不想生成太多的子类。
代码实现:
1     //Mediator
2    abstract class AbstractChatroom
3     {
4        public abstract void Register(Participant participant);
5        public abstract void Send(string from, string to, string message);
6     }

 1     //ConcreteMediator
 2     class Chatroom :AbstractChatroom
 3     {
 4         private Hashtable participants = new Hashtable();
 5         public override void Register(Participant participant)
 6         {
 7             if (participants[participant.Name] == null)
 8             {
 9                 participants[participant.Name]=participant;
10             }
11             participant.Chatroom = this;
12         }
13         public override void Send(string from, string to, string message)
14         {
15             Participant pto = (Participant)participants[to];
16             if (pto != null)
17             {
18                 pto.Receive(from, message);
19             }
20         }
21     }

 1     //AbstractColleague
 2     class Participant
 3     {
 4         private Chatroom chatroom;
 5         private string name;
 6         
 7         //Constructor
 8         public Participant(string name)
 9         {
10             this.name = name;
11         }
12         //Properties
13         public string Name
14         {
15             get { return name; }
16         }
17         public Chatroom Chatroom
18         {
19             set { chatroom = value; }
20             get { return chatroom; }
21           
22         }
23         public void Send(string to, string message)
24         {
25             chatroom.Send(name, to, message);
26         }
27         public virtual void Receive(string from, string message)
28         {
29             Console.WriteLine("{0} to {1}:'{2}'", from, name, message);
30         }
31     }

 1     //ConcreteColleaguel
 2     class Beatle :Participant
 3     {
 4      //Constructor
 5         public Beatle(string name)
 6             : base(name)
 7         { }
 8         public override void Receive(string from, string message)
 9         {
10             Console.Write("To a Beatle: ");
11             base.Receive(from, message);
12         }
13     }

 1     //ConcreteColleague2
 2     class NonBeatle :Participant
 3     {
 4         //Constructor
 5         public NonBeatle(string name)
 6             : base(name)
 7         { }
 8         public override void Receive(string from, string message)
 9         {
10             Console.Write("To a non-Beatle:");
11             base.Receive(from, message);
12         }
13     }
客户端调用如下:
 1 static void Main(string[] args)
 2         {
 3             //create chatroom
 4             Chatroom chatroom = new Chatroom();
 5             //Create participants and register them
 6             Participant George = new Beatle("George");
 7             Participant Paul = new Beatle("Paul");
 8             Participant Ringo = new Beatle("Ringo");
 9             Participant John = new Beatle("John");
10             Participant Yoko = new Beatle("Yoko");
11             chatroom.Register(George);
12             chatroom.Register(Paul);
13             chatroom.Register(Ringo);
14             chatroom.Register(John);
15             chatroom.Register(Yoko);
16 
17             //chatting participants
18             Yoko.Send("John""Hi John");
19             Paul.Send("Ringo""All you need is love");
20             Ringo.Send("George""My sweet Lord");
21             Paul.Send("John""Can't buy me love");
22             John.Send("Yoko""My sweet love");
23         }
运行结果如下:
           
Mediator实现要点:
    1.将多个对象间复杂的关联关系解耦,Mediator模式将多个对象间的控制逻辑进行集中管理,变“多个对象互相关系”为多“个对象和一个中介者关联”,简化了系统的维护,抵御了可能的变化。
    2.随着控制逻辑的复杂化,Mediator具体对象的实现可能相当复杂。 这时候可以对Mediator对象进行分解处理。
    3.Facade模式是解耦系统外到系统内(单向)的对象关系关系;Mediator模式是解耦系统内各个对象之间(双向)的关联关系。



posted on 2007-09-24 15:25  王晓成  阅读(5463)  评论(1编辑  收藏  举报