尽管将一个系统分割成许多对象通常可以增加其可服用性, 但是对象间相互连接的激增又会降低其可复用性了. 大量的连接使得一个对象不可能在没有改变其他对象的支持下工作, 系统表现为一个不可分割的整体, 所以, 对系统的行为进行任何较大的改动就十分困难了.

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

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
abstract class  {
public abstract void Send(string message, Colleague colleague);
} abstract class Colleague {
protected Mediator mediator; public Colleague(Mediator mediator) {
this.mediator = mediator;
}
} class ConcreteMediator : {
private ConcreteColleague1 colleague1;
private ConcreteColleague2 colleague2; public ConcreteColleague1 Colleague1 {
set {
colleague1 = value;
}
}
public ConcreteColleague2 Colleague2 {
set {
colleague2 = 大专栏  Design Patterns 25 class="keyword">value;
}
} public override void Send(string message, Colleague colleague) {
if(colleague == colleague1) {
colleague2.Notify(message);
}
else {
colleague1.Notify(message);
}
}
} class ConcreteColleague1 : Colleague {
public ConcreteColleague1(Mediator mediator) : base(mediator) { } public void Send(string message) {
mediator.Send(message, this);
} public void Notify(string message) { }
} class ConcreteColleague2 : Colleague {
public ConcreteColleague2(Mediator mediator) : base(mediator) { } public void Send(string message) {
mediator.Send(message, this);
} public void Notify(string message) { }
}

中介者模式优缺点

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

  • 优点
    1. Mediator的出现减少了各个Colleague的耦合, 使得可以独立的改变和复用各个Colleague类和Mediator.
    2. 由于把对象如何协作进行了抽象, 将中介作为一个独立的概念并将其封装在一个对象中, 这样关注的对象就从对象各自本身的行为转移到他们之间的交互上来, 也就是站在一个更宏观的角度去看待系统.
  • 缺点
    1. 由于ConcreteMediator控制了集中化, 于是就把交互复杂性变为了中介者的复杂性, 这就使得中介者会变得比任何一个ConcreteColleague都复杂.

中介者模式一般应用与一组对象已定义良好但是复杂的方式进行通信的场合, 以及想定制一个分布在多个类中的行为, 而又不想生成太多的子类的场合.

Design Patterns 25的更多相关文章

  1. Design Patterns Example Code (in C++)

    Overview Design patterns are ways to reuse design solutions that other software developers have crea ...

  2. Design Patterns in Android

    对日常在 Android 中实用设计模式进行一下梳理和总结,文中参考了一些网站和大佬的博客,如 MichaelX(xiong_it) .菜鸟教程.四月葡萄.IAM四十二等,在这里注明下~另外强烈推荐图 ...

  3. Design Patterns Simplified - Part 3 (Simple Factory)【设计模式简述--第三部分(简单工厂)】

    原文链接:http://www.c-sharpcorner.com/UploadFile/19b1bd/design-patterns-simplified-part3-factory/ Design ...

  4. Design Patterns Simplified - Part 2 (Singleton)【设计模式简述--第二部分(单例模式)】

    原文链接: http://www.c-sharpcorner.com/UploadFile/19b1bd/design-patterns-simplified-part-2-singleton/ De ...

  5. Head First Design Patterns

    From Head First Design Patterns. Design Principle: Idnetify the aspects of your application that var ...

  6. Apex Design Patterns

    Apex allows you to build just about any custom solution on the Force.com platform. But what are the ...

  7. [Design Patterns] 4. Creation Pattern

    设计模式是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结,使用设计模式的目的是提高代码的可重用性,让代码更容易被他人理解,并保证代码可靠性.它是代码编制真正实现工程化. 四个关键元素 ...

  8. [Design Patterns] 3. Software Pattern Overview

    When you're on the way which is unknown and dangerous, just follow your mind and steer the boat. 软件模 ...

  9. [Design Patterns] 1. Primary concept & term - UML

    It's time to review design patterns, especially when I reach the turning-point of my career. That's ...

随机推荐

  1. mysql安装(centos7)

    1.下载rpm wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm 2.安装rpm yum ...

  2. vue-cli3解决跨域问题

    在 vue.config.js里面配置 devServer: { proxy: { '/': { // search为转发路径 target: 'http://www.baidu.com', // 目 ...

  3. mysql 几个坑

    浮点转int类型 更新合并换成插入 不要频繁更新超过20个字节的字段 程序逻辑得到数据处理后才还回连接

  4. [GX/GZOI2019]旧词(树上差分+树剖+线段树)

    考虑k=1的做法:这是一道原题,我还写过题解,其实挺水的,但当时我菜还是看题解的:https://www.cnblogs.com/hfctf0210/p/10187947.html.其实就是树上差分后 ...

  5. mediawiki资料

    1.如何通过ip访问mediawiki --- http://blog.sina.com.cn/s/blog_3f2a2b8e01000awx.html 发布到外部网络,更改htfp.config里面 ...

  6. android课程表控件、悬浮窗、Todo应用、MVP框架、Kotlin完整项目源码

    Android精选源码 Android游戏2048 MVP Kotlin项目(RxJava+Rerotfit+OkHttp+Glide) Android基于自定义Span的富文本编辑器 android ...

  7. Codeforces 558E A Simple Task(计数排序+线段树优化)

    http://codeforces.com/problemset/problem/558/E Examples input 1 abacdabcda output 1 cbcaaaabdd input ...

  8. rest framework-版本-长期维护

    ###############  版本   ############### # # 版本的问题: # rest_framework.versioning.URLPathVersioning # 一般就 ...

  9. python爬虫和数据分析、数据挖掘

    一.python爬虫脑图: 二.python爬虫流程: 三.python数据分析简介 四.python数据预处理方法 五.python数据挖掘 六.数据探索基础

  10. 第一届“信安杯”部分WriteUp

    第一届"信安杯"部分WriteUp------OooooohLeeGay队! 小队成员(按姓氏):郭泽坤.李江川.赵乐祺 以下这部分是做出来的 2019.11.23-24 ++Re ...