[设计模式] 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() != )
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 = ; 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 ;
}
#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) = ; 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) = ;
}; class ConcreteMediator : public Mediator
{
public:
// The mediator forward the message
void 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 Colleague
ConcreteColleague1 *m_pColleague1;
ConcreteColleague2 *m_pColleague2;
}; void ConcreteColleague1::Send(wchar_t *message)
{
// The second parameter mark where the message comes from
m_pMediator->Sent(message, this);
} void ConcreteColleague2::Send(wchar_t *message)
{
m_pMediator->Sent(message, this);
} int main()
{
// Create the mediator
Mediator *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[] = L"Where are you from?";
pColleague1->Send(message); return ;
}
适用性:
1.一组对象以定义良好但是复杂的方式进行通信。产生的相互依赖关系结构混乱且难以理解。
2.一个对象引用其他很多对象并且直接与这些对象通信,导致难以复用该对象。
3.想定制一个分布在多个类中的行为,而又不想生成太多的子类。
优缺点:
使用中介者模式的优点:
1.降低了系统对象之间的耦合性,使得对象易于独立的被复用。
2.提高系统的灵活性,使得系统易于扩展和维护。
使用中介者模式的缺点:
由于我们这个“中介“承担了较多的责任,所以一旦这个中介对象出现了问题,那么整个系统就会受到重大的影响。
[设计模式] 17 中介者模式 Mediator Pattern的更多相关文章
- 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern)
原文:乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) 作者:weba ...
- 二十四种设计模式:中介者模式(Mediator Pattern)
中介者模式(Mediator Pattern) 介绍用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 示例有一个Messa ...
- 设计模式系列之中介者模式(Mediator Pattern)——协调多个对象之间的交互
说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...
- 中介者模式(Mediator Pattern)
用于减少多个对象或类之间的通信复杂性. 此模式提供了一个中介类,它通常处理不同类之间的所有通信,并支持通过松散耦合来维护代码.中介者模式属于行为模式类别. 实现实例 在这里通过一个聊天室的示例来演示中 ...
- 4.7 《硬啃设计模式》 第24章 麻烦的多角关系 - 中介者模式(Mediator Pattern)简介
在Windows程序中,有时候界面控件之间的交互会很麻烦,如:A控件显示什么的时候,B控件要显示什么,另外C控件要不可用,同样其它控件也会有类似的复杂要求.控件与控件之间很容易形成复杂的多角关系了.现 ...
- 23种设计模式--中介者模式-Mediator Pattern
一.中介者模式的介绍 中介者模式第一下想到的就是中介,房子中介,婚姻中介啊等等,当然笔者也希望来个婚姻中介给我介绍一个哈哈哈,,回归正题中介者模式分成中介者类和用户类,根据接口编程的方式我们再 ...
- 设计模式之中介者模式(Mediator)摘录
23种GOF设计模式一般分为三大类:创建型模式.结构型模式.行为模式. 创建型模式抽象了实例化过程.它们帮助一个系统独立于怎样创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而 ...
- 17中介者模式Mediator
一.什么是中介者模式 Mediator模式也叫中介者模式,是由GoF提出的23种 软件设计模式的一种.Mediator模式是行为模式之一, 在Mediator模式中,类之间的交互行为被统一放在 Med ...
- 18.中介者模式(Mediator Pattern)
using System; namespace Test { class Program { /// <summary> /// 中介者模式,定义了一个中介对象来封装一系列对象之间的交互关 ...
随机推荐
- cocos2d-x游戏循环与调度
每一个游戏程序都有一个循环在不断运行,它是有导演对象来管理很维护.如果需要场景中的精灵运动起来,我们可以在游戏循环中使用定时器(Scheduler)对精灵等对象的运行进行调度.因为Node类封装了Sc ...
- python:笔记for循环中的else
我们常常会在for循环遍历一个序列或者字典后,接着语句else,很多新手会误以为是判断执行else后面的 代码,其实不然,for循环里面也不存在判断,而已执行完遍历的对象后,再执行else后面的代码. ...
- 如何自学java迅速成为java高手
要想学好Java,首先要知道Java的大致分类.我们知道,自从Sun推出Java以来,就力图使之无所不包,所以Java发展到现在,按应用来分主要分为三大块:J2SE,J2ME和J2EE,这也就是Sun ...
- [Bootstrap]组件(三)
输入框组 添加额外元素.input-group-addon 外包元素.input-group>input-group-addon+form-control <div class=" ...
- Apache HTTP Server安装教程
Apache HTTP Server安装教程 Apache HTTP Server的官方网站是:http://httpd.apache.org/,可以从中下载最新版本的Apache HTTP Serv ...
- JQuery window.opener
$('#Save').click(function () { var parent = $(parent.document.body); $(parent).find('input#add ...
- php数组声明、遍历、数组全局变量使用小结
数组的本质:管理和操作一组变量,成批处理,下面为大家介绍下数组的分类.数组的分类及使用说明,感兴趣的朋友可以了解下哈 php教程:数组声明,遍历,数组全局变量 <? /* * 一.数组的概 ...
- linux下配置Apache基于加密的认证访问
1.首先要确认安装了 mod_ssl模块 我的机器是centos是系统,执行下面命令 yum install -y mod_ssl 2.用openssl工具生成密钥,证书请求文件,证书 在/usr/l ...
- Android ADB server didn't ACK * failed to start daemon * 简单有效的解决方案
转载请注明出处:http://blog.csdn.net/xiaanming/article/details/9401981 ADB server didn't ACK 这个问题会困恼很多的新手朋友, ...
- Excel REPT函数使用
需要制作1K大小的数据 使用Excel REPT函数可以迅速制造 Excel REPT 函数 =REPT(1,1024) 结果直接黏贴进txt文件,注意删除尾空格.