原文 第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

第17章 中介者模式(Mediator Pattern)的更多相关文章

  1. 二十四种设计模式:中介者模式(Mediator Pattern)

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

  2. 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern)

    原文:乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) 作者:weba ...

  3. 中介者模式(Mediator Pattern)

    用于减少多个对象或类之间的通信复杂性. 此模式提供了一个中介类,它通常处理不同类之间的所有通信,并支持通过松散耦合来维护代码.中介者模式属于行为模式类别. 实现实例 在这里通过一个聊天室的示例来演示中 ...

  4. 设计模式系列之中介者模式(Mediator Pattern)——协调多个对象之间的交互

    说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...

  5. [设计模式] 17 中介者模式 Mediator Pattern

    在GOF的<设计模式:可复用面向对象软件的基础>一书中对中介者模式是这样说的:用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变 ...

  6. 4.7 《硬啃设计模式》 第24章 麻烦的多角关系 - 中介者模式(Mediator Pattern)简介

    在Windows程序中,有时候界面控件之间的交互会很麻烦,如:A控件显示什么的时候,B控件要显示什么,另外C控件要不可用,同样其它控件也会有类似的复杂要求.控件与控件之间很容易形成复杂的多角关系了.现 ...

  7. 18.中介者模式(Mediator Pattern)

    using System; namespace Test { class Program { /// <summary> /// 中介者模式,定义了一个中介对象来封装一系列对象之间的交互关 ...

  8. 23种设计模式--中介者模式-Mediator Pattern

    一.中介者模式的介绍     中介者模式第一下想到的就是中介,房子中介,婚姻中介啊等等,当然笔者也希望来个婚姻中介给我介绍一个哈哈哈,,回归正题中介者模式分成中介者类和用户类,根据接口编程的方式我们再 ...

  9. 十一个行为模式之中介者模式(Mediator Pattern)

    定义: 用一个中介对象(中介者)来封装一系列的对象交互,使各个对象之间不需要显式地相互引用,从而降低耦合度,而且可以独立地改变他们之间的交互关系. 解耦后: 结构图: Mediator:抽象中介者,定 ...

随机推荐

  1. Android 省市县 三级联动(android-wheel的使用)

    转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/23382805 今天没事跟群里面侃大山,有个哥们说道Android Wheel这个 ...

  2. 【UVA272】TEX Quotes

    A - TEX Quotes Time Limit:3000MS    Memory Limit:0KB    64bit IO Format:%lld & %llu Submitcid=80 ...

  3. hdu1325 Is It A Tree?并检查集合

    pid=1325">职务地址 试想一下,在词和话题hdu1272是一样的. 可是hdu1272的博文中我也说了.数据比較水,所以我用非并查集的方法就AC了. 可是这题的数据没那么水,要 ...

  4. maven插件的生命周期的详细说明(两)

    插件配置 定义解释:插件目标 当我们了解了maven插件之后.我们发现假设为每个功能编写一个独立的插件显然是不可取的,由于这些任务背后有非常多能够复用的代码.因此,把这些功能聚集在一个插件里,每个功能 ...

  5. RH133读书 笔记(3) - Lab 3 Configuring the kernel

    Lab 3 Configuring the kernel Goal: Develop skills tuning the /proc filesystem. Gain some experience ...

  6. angular input使用输入框filter格式化日期

    最近使用angular日期选取器.只需要把所选的输出迄今input输入框,根据默认的假设,显示是在时间的形式的时间戳.不符合规定.需要格成一个特定的公式格公式.但input上ng-model不能直接对 ...

  7. 设计模式之前奏(UML类图)

    原文:设计模式之前奏(UML类图) 本人菜菜一个,最近一直在博客园游走闲逛,看到了各种技术,各种各种…….便看到了大话设计模式这本书,下了电子版的看了看第一章,感觉相当不错,不仅通俗易懂,而且与实际案 ...

  8. Binomial Coeffcients 过去山东省省赛冠军

    Binomial Coeffcients Time Limit: 1000MS Memory limit: 65536K 题目描写叙述   输入   输出   演示样例输入 3 1 1 10 2 95 ...

  9. RH253读书笔记(5)-Lab 5 Network File Sharing Services

    Lab 5 Network File Sharing Services Goal: Share file or printer resources with FTP, NFS and Samba Se ...

  10. 论docker中 CMD 与 ENTRYPOINT 的区别(转)

    Dockerfile 用于自动化构建一个docker镜像.Dockerfile里有 CMD 与 ENTRYPOINT 两个功能咋看起来很相似的指令,开始的时候觉得两个互用没什么所谓,但其实并非如此: ...