设计模式之二十一:中介者模式(Mediator)
中介者模式:定义了一个对象。用来封装一系列对象的交互。中介者模式通过使对象之间不必显式引用减少了对象之间的耦合,而且同意你独立改变它们之间的交互。
中介者模式就是将对象之间的交互封装在了一个独立的对象中。这个独立的对象用来控制对象之间的交互行为,所以这个对象还是蛮复杂的。
UML类图:
主要包含:
- Mediator:定义了一个Colleague对象之间交互的接口。
- ConcreteMediator:实现了Colleague对象之间的交互行为,并了解和能操作Colleague对象。
- Colleague classes:每个Colleague都认识Mediator对象。而且通过Mediator来实现彼此之间的通讯。
基础C++代码例如以下:
#include <iostream>
#include <string>
using namespace std;
class Colleague;
class Mediator
{
public:
virtual void send(string message,Colleague * c)=0;
};
class Colleague
{
public:
Colleague(Mediator *m)
{
mediator=m;
}
void send(string message)
{
mediator->send(message,this);
}
virtual void notify(string message)=0;
protected:
Mediator * mediator;
};
class ConcreteColleague1:public Colleague
{
public:
ConcreteColleague1(Mediator * c):Colleague(c)
{
}
void notify(string message)
{
cout<<"concreteColleague1 receive message:"<<message<<endl;
}
};
class ConcreteColleague2:public Colleague
{
public:
ConcreteColleague2(Mediator *m):Colleague(m)
{
}
void notify(string message)
{
cout<<"concreteColleague2 receive message:"<<message<<endl;
}
};
class ConcreteMediator:public Mediator
{
public:
void send(string message,Colleague * c)
{
ConcreteColleague1* c1=dynamic_cast<ConcreteColleague1 *>(c);
ConcreteColleague2* c2=dynamic_cast<ConcreteColleague2 *>(c);
if(c1!=NULL)
{
cc2->notify(message);
}
if(c2!=NULL)
{
cc1->notify(message);
}
}
void setCC1(ConcreteColleague1 * c)
{
cc1=c;
}
void setCC2(ConcreteColleague2 * c)
{
cc2=c;
}
private:
ConcreteColleague1 * cc1;
ConcreteColleague2 * cc2;
};
int main()
{
ConcreteMediator *m=new ConcreteMediator();
ConcreteColleague1 * cc1=new ConcreteColleague1(m);
ConcreteColleague2 * cc2=new ConcreteColleague2(m);
m->setCC1(cc1);
m->setCC2(cc2);
cc1->send("how are you !");
cc2->send("fine ,thank you");
return 0;
}
运行输出:
一个使用中介者模式的聊天室的样例。
这里实现了Mediator和Colleague的一对多的聚合关系。这种中介者模式才是有意义的使用方式。
UML类图:
C++代码例如以下:
#include <iostream>
#include <map>
#include <string>
using namespace std;
class Participant;
class AbstractChatRoom
{
public:
virtual void registe(Participant * p)=0;
virtual void send(string from,string to ,string message)=0;
};
class ChatRoom:public AbstractChatRoom
{
public:
void registe(Participant * p);
void send(string from,string to ,string message);
private:
map<Participant *,string> participants;
};
class Participant
{
public:
Participant(string n=""):name(n)
{
}
string getName()
{
return name;
}
void setChatRoom(ChatRoom *c)
{
chatRoom=c;
}
void send(string to,string message)
{
chatRoom->send(name,to,message);
}
virtual void receive(string from,string message)
{
cout<<from<<" to "<<name<<" : "<<message<<endl;
}
private:
string name;
ChatRoom * chatRoom;
};
void ChatRoom::registe(Participant * p)
{
if(!participants.count(p))
{
participants.insert(pair<Participant *,string>(p,p->getName()));
}
p->setChatRoom(this);
}
void ChatRoom::send(string from,string to ,string message)
{
map<Participant *,string>::iterator iter;
for(iter=participants.begin();iter!=participants.end();iter++)
{
if(iter->second==to)
break;
}
if(iter!=participants.end())
{
iter->first->receive(from,message);
}
}
class Beatle:public Participant
{
public:
Beatle(string n=""):Participant(n)
{
}
void receive(string from,string message)
{
cout<<"to a beatle : ";
Participant::receive(from,message);
}
};
class NonBeatle:public Participant
{
public:
NonBeatle(string n=""):Participant(n)
{
}
void receive(string from,string message)
{
cout<<"to a non-beatle : ";
Participant::receive(from,message);
}
};
int main()
{
cout<<"聊天室中介者模式代码"<<endl;
ChatRoom * chatRoom=new ChatRoom();
Participant *george=new Beatle("George");
Participant *paul=new Beatle("Paul");
Participant *ringo=new Beatle("Ringo");
Participant *john=new Beatle("John");
Participant *yoko=new NonBeatle("Yoko");
chatRoom->registe(george);
chatRoom->registe(paul);
chatRoom->registe(ringo);
chatRoom->registe(john);
chatRoom->registe(yoko);
yoko->send("John","hi John!");
paul->send("Ringo","All you need is love");
ringo->send("George","My sweet Lord");
paul->send("John","can not buy me love");
john->send("Yoko","My sweet love");
return 0;
}
运行输出:
设计模式之二十一:中介者模式(Mediator)的更多相关文章
- C#设计模式之二十一职责链模式(Chain of Responsibility Pattern)【行为型】
一.引言 今天我们开始讲"行为型"设计模式的第八个模式,该模式是[职责链模式],英文名称是:Chain of Responsibility Pattern.让我们看看现实生活中 ...
- 设计模式(二十一)Proxy模式
在面向对象编程中,“本人”和“代理人”都是对象.如果“本人”对象太忙了,有些工作无法自己亲自完成,就将其交给“代理人”对象负责. 示例程序的类图. 示例程序的时序图.从这个时序图可以看出,直到调用pr ...
- 设计模式(二十一)——解释器模式(Spring 框架中SpelExpressionParser源码分析)
1 四则运算问题 通过解释器模式来实现四则运算,如计算 a+b-c 的值,具体要求 1) 先输入表达式的形式,比如 a+b+c-d+e, 要求表达式的字母不能重复 2) 在分别输入 a ,b, c, ...
- 中介者模式 调停者 Mediator 行为型 设计模式(二十一)
中介者模式(Mediator) 调度.调停 意图 用一个中介对象(中介者)来封装一系列的对象交互,中介者使各对象不需要显式地相互引用,从而使其耦合松散 而且可以独立地改变它们之间的交互. ...
- 二十四种设计模式:中介者模式(Mediator Pattern)
中介者模式(Mediator Pattern) 介绍用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 示例有一个Messa ...
- C#设计模式之十八中介者模式(Mediator Pattern)【行为型】
一.引言 今天我们开始讲“行为型”设计模式的第五个模式,该模式是[中介者模式],英文名称是:Mediator Pattern.还是老套路,先从名字上来看看.“中介者模式”我第一次看到这个名称,我的理解 ...
- 设计模式的征途—22.中介者(Mediator)模式
我们都用过QQ,它有两种聊天方式:一是私聊,二是群聊.使用QQ群,一个用户就可以向多个用户发送相同的信息和文件,从而无需一一发送,节省大量时间.通过引入群的机制,极大地减少系统中用户之间的两两通信,用 ...
- 【转】设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)
设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据"单一职责原则",我们应该尽量将对象细化,使其只负责或呈现单一的职 ...
- 设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)
设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据“单一职责原则”,我们应该尽量将对象细化,使其只负责或呈现单一的职责,即将行为分布到各 ...
随机推荐
- html-定位
概述: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- 【官档整理】Visual Studio 2017 VS2017 中文离线安装包下载
[官档整理]Visual Studio 2017 VS2017 中文离线安装包下载 转 https://blog.csdn.net/fromfire2/article/details/81104648 ...
- Gym 102091K The Stream of Corning 2【线段树】
<题目链接> 题目大意: 进行两种操作:1.给定一个数的出现时间.价值.消失时间: 2.进行一次询问,问你当前时间,第K大的数的价值. 解题分析: 采用离线集中处理,将每个数的出现时间和它 ...
- P4781 【模板】拉格朗日插值
P4781 [模板]拉格朗日插值 证明 :https://wenku.baidu.com/view/0f88088a172ded630b1cb6b4.html http://www.ebola.pro ...
- 输出日文CSV乱码问题
直接写用Excel打开时会乱码,需要加上下面代码中注释的三行 fos = new FileOutputStream(file, false); //fos.write( 0xef ); //fos.w ...
- ENQUIRE the predecessor to the World Wide Web.
看Building Responsive Data Visualization for the Web时介绍到了Enquire,表示wiki类系统实现了它的核心思想. 有点好奇是如何实现的,所以大概看 ...
- BZOJ.4738.[清华集训2016]汽水(点分治 分数规划)
BZOJ UOJ 记\(val_i\)是每条边的边权,\(s\)是边权和,\(t\)是经过边数,\(k\)是给定的\(k\). 在点分治的时候二分答案\(x\),设\(|\frac st-k|=x\) ...
- 【整理】Java 10新特性总结
Java 9才发布几个月,很多玩意都没整明白,Java 10就来了..这时候我真尼玛想说:线上用的JDK 7 ,JDK 8 还没用熟,JDK 9 才发布不久不知道啥玩意,JDK 10……刚学Java的 ...
- 【开源GPS追踪】 之 服务器硬伤
前面就说过了,目前GPS 追踪的原理都是通过GPRS将数据发送到一个服务器上,如果回看数据就从服务器上去数据,服务器在整个系统中具有举足轻重的地位. 如果服务器坏了,整个系统几千台设备可能也就无法工作 ...
- the lime limited error
转载自:https://blog.csdn.net/MTOY_320/article/details/78363375?locationNum=7&fps=1 经常会遇到这种令人抓狂的情况 自 ...