[设计模式] 17 中介者模式 Mediator Pattern
在GOF的《设计模式:可复用面向对象软件的基础》一书中对中介者模式是这样说的:用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
我们都知道,面向对象设计鼓励将行为分布到各个对象中。但是,这种分布可能会导致对象间有许多连接。在最坏的情况下,每一个对象都知道其他所有对象,就造成了复杂的关联关系。虽然将一个系统分割成许多对象通常可以增强可复用性,但是对象间相互连接的激增又会降低其可复用性。大量的相互连接使得一个对象似乎不太可能在没有其他对象的支持下工作,这样使得系统表现为一个不可分割的整体。而且,对系统的行为进行任何较大的改动都十分困难,因为行为被分布在许多对象中。结果是,你可能不得不定义很多子类以定制系统的行为。
问题再回到联合国的问题上来,在联合国还没有成立时,国与国之间的关系是这样的:
当联合国成立以后,国与国之间出现纠纷时,是这样的:
联合国的成立,让很多关系简单化了,让问题的处理也简单化了,使国与国之间因为纠纷产生摩擦的几率减小了,让世界更和平了。
中介者模式就是定义一个中介对象来封装系列对象之间的交互。中介者使各个对象不需要显示地相互引用,从而使其耦合性松散,而且可以独立地改变他们之间的交互。
类图和实例
Mediator类:抽象中介者,定义了同事对象交互的接口。
ConcreteMediator类:具体中介者对象,实现抽象类中的方法,此具体中介者对象需要知道所有具体同事类,并从具体同事接受消息,向具体同事对象发送命令。
Colleague类:抽象同事类。
ConcreteColleague类:具体同事类,实现抽象同事类中的方法。每一个同时类需要知道中介者对象;每个具体同事类只需要了解自己的行为,而不需要了解其他同事类的情况。
#include <iostream> #include <vector> #include <string>using namespace std;class Colleage { private:string name;string content; public:Colleage(string n = " "):name(n){};void set_name(string name){this->name = name;}string get_name(){return this->name;}void set_content(string content){this->content = content;}string get_content(){if(content.size() != 0)return content;else return "Copy that";}virtual void talk(){};};class Monitor : public Colleage { public:Monitor(string n = ""):Colleage(n){};virtual void talk(){cout<<"班长 "<<get_name()<<" 说:"<<get_content()<<endl;} };class Secretary : public Colleage { public:Secretary(string n = ""):Colleage(n){};virtual void talk(){cout<<"团支书 "<<get_name()<<" 说:"<<get_content()<<endl;} };class StudentA : public Colleage { public:StudentA(string n = ""):Colleage(n){};virtual void talk(){cout<<"学生 A "<<get_name()<<" 说:"<<get_content()<<endl;} };class StudentB : public Colleage { public:StudentB(string n = ""):Colleage(n){};virtual void talk(){cout<<"学生 B "<<get_name()<<" 说:"<<get_content()<<endl;} };class Mediator { public:vector<Colleage*> studentList;virtual void add_student(Colleage *student){studentList.push_back(student);};virtual void notify(Colleage *student){}; };class QQMediator : public Mediator { public:virtual void notify(Colleage *student){student->talk();for(int i = 0 ; i < studentList.size() ; ++i){ if(student != studentList[i]){studentList[i]->talk();}}}; };int main() {QQMediator qq;Monitor *studentMonitor = new Monitor("Foxx");Secretary *studentSecretary = new Secretary("TC");StudentA *studentA = new StudentA("Jack");StudentB *studentB = new StudentB("Frank"); qq.add_student(studentSecretary);qq.add_student(studentA);qq.add_student(studentB); studentMonitor->set_content("明天开始放假!");qq.notify(studentMonitor); return 0; }
#include <iostream> using namespace std;#define SAFE_DELETE(p) if (p) { delete p; p = NULL; }class Mediator;class Colleague { public:Colleague(Mediator *pMediator) : m_pMediator(pMediator){}virtual void Send(wchar_t *message) = 0;protected:Mediator *m_pMediator; };class ConcreteColleague1 : public Colleague { public:ConcreteColleague1(Mediator *pMediator) : Colleague(pMediator){}void Send(wchar_t *message);void Notify(wchar_t *message){wcout<<message<<endl;} };class ConcreteColleague2 : public Colleague { public:ConcreteColleague2(Mediator *pMediator) : Colleague(pMediator){}void Send(wchar_t *message);void Notify(wchar_t *message){cout<<"ConcreteColleague2 is handling the message."<<endl;wcout<<message<<endl;} };class Mediator { public:virtual void Sent(wchar_t *message, Colleague *pColleague) = 0; };class ConcreteMediator : public Mediator { public:// The mediator forward the messagevoid Sent(wchar_t *message, Colleague *pColleague){ConcreteColleague1 *pConcreteColleague1 = dynamic_cast<ConcreteColleague1 *>(pColleague);if (pConcreteColleague1){cout<<"The message is from ConcreteColleague1. Now mediator forward it to ConcreteColleague2"<<endl;if (m_pColleague2){m_pColleague2->Notify(message);}}else{if (m_pColleague1){m_pColleague1->Notify(message);}}}void SetColleague1(Colleague *pColleague){m_pColleague1 = dynamic_cast<ConcreteColleague1 *>(pColleague);}void SetColleague2(Colleague *pColleague){m_pColleague2 = dynamic_cast<ConcreteColleague2 *>(pColleague);}private:// The Mediator knows all the ColleagueConcreteColleague1 *m_pColleague1;ConcreteColleague2 *m_pColleague2; };void ConcreteColleague1::Send(wchar_t *message) {// The second parameter mark where the message comes fromm_pMediator->Sent(message, this); }void ConcreteColleague2::Send(wchar_t *message) {m_pMediator->Sent(message, this); }int main() {// Create the mediatorMediator *pMediator = new ConcreteMediator();Colleague *pColleague1 = new ConcreteColleague1(pMediator);Colleague *pColleague2 = new ConcreteColleague2(pMediator);ConcreteMediator *pConcreteMediator = dynamic_cast<ConcreteMediator *>(pMediator);pConcreteMediator->SetColleague1(pColleague1);pConcreteMediator->SetColleague2(pColleague2);wchar_t message[260] = L"Where are you from?";pColleague1->Send(message);return 0; }
适用性:
1.一组对象以定义良好但是复杂的方式进行通信。产生的相互依赖关系结构混乱且难以理解。
2.一个对象引用其他很多对象并且直接与这些对象通信,导致难以复用该对象。
3.想定制一个分布在多个类中的行为,而又不想生成太多的子类。
优缺点:
使用中介者模式的优点:
1.降低了系统对象之间的耦合性,使得对象易于独立的被复用。
2.提高系统的灵活性,使得系统易于扩展和维护。
使用中介者模式的缺点:
由于我们这个“中介“承担了较多的责任,所以一旦这个中介对象出现了问题,那么整个系统就会受到重大的影响。
[设计模式] 17 中介者模式 Mediator Pattern相关推荐
- 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern)
[索引页] [源码下载] 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) 作者:webabcd 介绍 用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互 ...
- PHP设计模式之中介者模式(Mediator Pattern)了解下
咱们先来看下中介者模式(Mediator Pattern)的定义,它就是,用一个中介对象来封装一系列的对象交互,中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互, ...
- 设计模式之中介者模式---Mediator Pattern
模式的定义 中介者模式定义如下: Define an object that encapsulates how a set of objects interact.Mediator promotes ...
- 【23种设计模式】中介者模式(Mediator Pattern) .Net Core实现
文章目录 简介 伪代码演示 简单实现 使用MediatR实现中介者模式 来源 简介 中介者模式(Mediator Pattern)是用来降低多个对象和类之间的通信复杂性.这种模式提供了一个中介类,该类 ...
- 设计模式之中介者模式(Mediator Pattern)
中介者模式定义 Define an object that encapsulates how a set of objects interact. Mediator promotes loose co ...
- 设计模式-中介者模式(Mediator Pattern)
设计模式-中介者模式(Mediator Pattern) 文章目录 设计模式-中介者模式(Mediator Pattern) 一.定义 二.概念解释 三.场景 四.实现 1.类图 2.代码实现 五.总 ...
- java中介者模式例子_Java中介者模式(Mediator Pattern)
本篇文章帮大家学习java中介者模式(Mediator Pattern),包含了Java中介者模式(Mediator Pattern)使用方法.操作技巧.实例演示和注意事项,有一定的学习价值,大家可以 ...
- 7.7 中介者模式(Mediator Pattern)
一. 定义 在现实生活中,常出现多个对象之间存在复杂的交互关系,这种交互关系常常是"网状结构",要求每个对象都必须知道它需要交互的对象.如:每个人必须记住他所有朋友的电话,若朋友中 ...
- 设计模式之中介者模式(Mediator)摘录
23种GOF设计模式一般分为三大类:创建型模式.结构型模式.行为模式. 创建型模式抽象了实例化过程,它们帮助一个系统独立于如何创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而 ...
最新文章
- 使用Silverlight for Embedded开发绚丽的界面(4)
- QML从右到左的用户界面
- OpenGL之矩阵的基本变换和矩阵堆栈
- android飞翔的小鸟游戏素材包_开心消消乐×愤怒的小鸟:为开心而战
- [BZOJ 1026] [SCOI 2009] Windy数 【数位DP】
- 统计iOS项目的总代码行数的方法
- QML笔记-对QML中信号与槽的进一步认识
- CSS3的radial-gradient(径向渐变)
- linux下的多线程
- 算捡漏么?我发现了一个值2万美金的 Facebook DOM XSS 漏洞
- python生成器和迭代器区别_Python_生成器和迭代器的区别
- 【NOIP 模拟赛】Evensgn 剪树枝 树形dp
- IMX8基于FFT的GPU和CPU的性能测试
- 锐起3.1无盘服务器,[迎新春]锐起3.1无盘XP万能包13V2(IE8版本)
- 低版本浏览器不支持HTML5标签怎么解决?
- 视频素材-高质量缥缈雾气雾霾特效合成动画 Lens Distortions – Fog II
- ptp精准时间协议_精确时间协议PTP研究
- 用html5做课件,这么好玩!PPT竟然还能用来制作H5?
- 华为南研所2014春季机试题目-1字符串拼接
- 2012年5月SAT香港真题解析