设计模式之二十一:中介者模式(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.概述 在面向对象的软件设计与开发过程中,根据“单一职责原则”,我们应该尽量将对象细化,使其只负责或呈现单一的职责,即将行为分布到各 ...
随机推荐
- python 精确计算与向上取整 decimal math.ceil
1. 精确计算 python的float型不精确,需要导入decimal包,以下是不精确举例: 导入decimal包后: 2. 向上取整 一般的取整数(向下取整): 向上取整的方法:
- win10下正确使用Sublime Text搭建python调试环境
pycharmt等IDE虽然用着爽,但毕竟在速度.资源上还是比较让人不爽的. 使用IDE无非是图个方便省事,特别是像我这种有些记性差的来说. IDE说起来方便于的几个地方就是: 1.语法颜色高亮 ...
- BZOJ 4260 Codechef REBXOR (区间异或和最值) (01字典树+DP)
<题目链接> 题目大意:给定一个序列,现在求出两段不相交的区间异或和的最大值. 解题分析: 区间异或问题首先想到01字典树.利用前缀.后缀建树,并且利用异或的性质,相同的两个数异或变成0, ...
- 常见素数筛选方法原理和Python实现
1. 普通筛选(常用于求解单个素数问题) 自然数中,除了1和它本身以外不再有其他因数. import math def func_get_prime(n): func = lambda x: not ...
- 向cmd中添加字体的方法
首先下载字体到C:\Windows\Fonts中,然后参考 http://blog.csdn.net/bbirdsky/article/details/38495661 中所讲的方法进行添加.
- ;(function(){ //代码})(); 自执行函数开头为什么要加;或者!
我们有时候会在自执行函数中看到这样的代码;(function(){ //我们的代码.. alert('Hello!'); })(); !(function(){ //我们的代码.. alert('He ...
- [算法]Collebarative Filtering
挖坑 https://en.wikipedia.org/wiki/Collaborative_filtering
- go defer笔记
1.函数中return xxx非原子指令 2.函数返回过程:先给返回值赋值:再调用defer:最后回到调用函数中 即:返回值 = xxx; defer; return; 3.多个defer调用顺序类似 ...
- me 云面试
元祖的特点: 1.元组内的元素,不可以增加,删除,只能访问,这个是元祖的特性,比较安全.类似于字符串.但是我们可以对整个元祖进行删除.使用del内置函数 2.当元祖内只有一个元素的时候,需要加逗号消除 ...
- 登录页面加密token和盐的作用
盐: 可以说盐是作用于注册,盐就是将用户输入的原始密码,加密后存进数据库,然后把盐(实际上是随机字符串)也存进数据库 Map<String, String> map = new HashM ...