第17章 中介者模式(Mediator Pattern)

简介: 原文 第17章 中介者模式(Mediator Pattern) 中介者模式        概述:        在软件开发中,我们有时会碰上许多对象互相联系互相交互的情况,对象之间存在复杂的引用关系,当需求更改时,对系统进行修改将会非常困难。

原文 第17章 中介者模式(Mediator Pattern)

中介者模式

 

     概述:

 

     在软件开发中,我们有时会碰上许多对象互相联系互相交互的情况,对象之间存在复杂的引用关系,当需求更改时,对系统进行修改将会非常困难。为了对系统的对象进行解耦,可以引入一个间接层来管理对象之间的关系,这就是中介者模式。

     

    结构图:

 

 

 

      借图理解:

   使用中介者

   

 

 

 

       示例:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
   /// <summary>
     /// 抽象中介者
     /// </summary>
     public  abstract  class  Mediator
     {
         public  Colleague _colleague;
         public  Colleague GetColleague()
         {
             return  _colleague;
 
         }
         public  void  SetColleague(Colleague colleague)
         {
             _colleague = colleague;
         }
         public  abstract  void  DoSomething();
 
     }
     /// <summary>
     /// 具体的中介者
     /// </summary>
     public  class  ConcreteMediator : Mediator
    
      public  override  void  DoSomething()
      {
          //调用Colleague对象的方法
          _colleague.Method();
      }
     }
     /// <summary>
     /// 抽象Colleague
     /// </summary>
     public  abstract  class  Colleague
     {
        public  abstract  void  Method();
     }
     /// <summary>
     /// 具体的Colleague
     /// </summary>
     public  class  ConcreteColleague1:Colleague
     {
 
         private  Mediator mediator;
         public  ConcreteColleague1(Mediator mediator)
         {
             this .mediator = mediator;
         }
         public  override  void  Method()
         {
             Console.WriteLine( "ConcreteColleague1要做的事!" );
         }
     }
     /// <summary>
     /// 具体的Colleague
     /// </summary>
     public  class  ConcreteColleague2 : Colleague
     {
 
         private  Mediator mediator;
         public  ConcreteColleague2(Mediator mediator)
         {
             this .mediator = mediator;
         }
         public  override  void  Method()
         {
             Console.WriteLine( "ConcreteColleague2要做的事!" );
         }
     }

客户端调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
     class  Program
     {
         static  void  Main( string [] args)
         {
 
             Mediator mediator =  new  ConcreteMediator();
 
             Colleague coll1 =  new  ConcreteColleague1(mediator);
             mediator.SetColleague(coll1);
             coll1.Method();
             Console.ReadLine();
 
             Colleague coll2 =  new  ConcreteColleague2(mediator);
             mediator.SetColleague(coll2);
             coll2.Method();
             Console.ReadLine();
         }
     }

 

    效果:

    

        1.降低了系统对象之间的耦合性,使得对象易于独立的被复用。

        2.提高系统的灵活性,使得系统易于扩展和维护。

 

    适用场景:

     

         1.一组对象以定义良好但是复杂的方式进行通信。产生的相互依赖关系结构混乱且难以理解。 

         2.一个对象引用其他很多对象并且直接与这些对象通信,导致难以复用该对象。

         3.想定制一个分布在多个类中的行为,而又不想生成太多的子类。

 

     设计模式系列文章入口:http://www.diyibk.com/post/39.html

目录
相关文章
|
6月前
|
设计模式 前端开发
设计模式21 - 中介者模式【【Mediator Pattern】
设计模式21 - 中介者模式【【Mediator Pattern】
23 0
|
3月前
|
设计模式 算法
设计模式 - 行为型模式_ 访问者模式Visitor Pattern
设计模式 - 行为型模式_ 访问者模式Visitor Pattern
38 1
设计模式 - 行为型模式_ 访问者模式Visitor Pattern
|
6月前
|
设计模式 BI
设计模式18 - 访问者模式【Visitor Pattern】
设计模式18 - 访问者模式【Visitor Pattern】
22 0
|
9月前
|
设计模式 Java uml
中介者模式(Mediator Pattern)
中介者模式(Mediator Pattern)是一种行为型设计模式,它用于降低多个对象之间的耦合度,通过引入一个中介者对象,将对象之间的交互转化为中介者和对象之间的交互,从而避免了对象之间的直接耦合。
63 1
|
算法 Java 编译器
行为型模式 - 访问者模式(Visitor Pattern)
行为型模式 - 访问者模式(Visitor Pattern)
|
存储 Java 程序员
行为型模式 - 备忘录模式(Memento Pattern)
行为型模式 - 备忘录模式(Memento Pattern)
|
存储 设计模式 安全
结构型模式 - 组合模式(Composite Pattern)
结构型模式 - 组合模式(Composite Pattern)
|
应用服务中间件 智能硬件 容器
结构型模式 - 外观模式(Facade Pattern)
结构型模式 - 外观模式(Facade Pattern)
结构型模式 - 装饰器模式(Decorator Pattern)
结构型模式 - 装饰器模式(Decorator Pattern)
|
存储 缓存 Java
结构型模式 - 享元模式(Flyweight Pattern)
结构型模式 - 享元模式(Flyweight Pattern)