[设计模式] 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> /// 中介者模式,定义了一个中介对象来封装一系列对象之间的交互关 ...
随机推荐
- (转)RabbitMQ消息队列(七):适用于云计算集群的远程调用(RPC)
在云计算环境中,很多时候需要用它其他机器的计算资源,我们有可能会在接收到Message进行处理时,会把一部分计算任务分配到其他节点来完成.那么,RabbitMQ如何使用RPC呢?在本篇文章中,我们将会 ...
- JavaScript写选项卡
方法一: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- Codevs 1083 Cantor表
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的 ...
- C# 获取汉字的拼音首字母
/// <summary> /// 在指定的字符串列表CnStr中检索符合拼音索引字符串 /// </summary> /// <param name="CnS ...
- 为什么要用ajax
Ajax应用程序的优势在于:1. 通过异步模式,提升了用户体验2. 优化了浏览器和服务器之间的传输,减少不必要的数据往返,减少了带宽占用3. Ajax引擎在客户端运行,承担了一部分本来由服务器承担的工 ...
- 让ImageView可以使用gif的方法
在自己的包中添加MyGifView.java(直接复制,粘贴),读取gif资源在MyGifView中第20行读取: MyGifView.java: package com.zzw.testgifuse ...
- Delphi XE5教程8:使用Delphi命名空间
// Project file declarations... //项目文件声明… program MyCompany.ProjectX.ProgramY; // Unit source file d ...
- Django模版进阶
# -*- coding: utf-8 -*-from django.shortcuts import renderdef home(request): string = "测试" ...
- Java读取本地文件进行unicode解码
工具使用: package test.opservice; import eh.util.MapValueUtil; import java.io.BufferedReader; import jav ...
- Java Day 02
关键字 都是小写,类名首字母大写 标识符 1.数字不可以开头 2.不可以使用关键字 区分大小写 26个大小写字母.0-9._.$ 组成 main是关键字么? 注释 单行注释 // 多行注释 /* */ ...