中介者模式:定义了一个对象。用来封装一系列对象的交互。中介者模式通过使对象之间不必显式引用减少了对象之间的耦合,而且同意你独立改变它们之间的交互。

中介者模式就是将对象之间的交互封装在了一个独立的对象中。这个独立的对象用来控制对象之间的交互行为,所以这个对象还是蛮复杂的。

UML类图:

主要包含:

  1. Mediator:定义了一个Colleague对象之间交互的接口。
  2. ConcreteMediator:实现了Colleague对象之间的交互行为,并了解和能操作Colleague对象。
  3. 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)的更多相关文章

  1. C#设计模式之二十一职责链模式(Chain of Responsibility Pattern)【行为型】

    一.引言   今天我们开始讲"行为型"设计模式的第八个模式,该模式是[职责链模式],英文名称是:Chain of Responsibility Pattern.让我们看看现实生活中 ...

  2. 设计模式(二十一)Proxy模式

    在面向对象编程中,“本人”和“代理人”都是对象.如果“本人”对象太忙了,有些工作无法自己亲自完成,就将其交给“代理人”对象负责. 示例程序的类图. 示例程序的时序图.从这个时序图可以看出,直到调用pr ...

  3. 设计模式(二十一)——解释器模式(Spring 框架中SpelExpressionParser源码分析)

    1 四则运算问题 通过解释器模式来实现四则运算,如计算 a+b-c 的值,具体要求 1) 先输入表达式的形式,比如 a+b+c-d+e,  要求表达式的字母不能重复 2) 在分别输入 a ,b, c, ...

  4. 中介者模式 调停者 Mediator 行为型 设计模式(二十一)

      中介者模式(Mediator)   调度.调停   意图 用一个中介对象(中介者)来封装一系列的对象交互,中介者使各对象不需要显式地相互引用,从而使其耦合松散 而且可以独立地改变它们之间的交互. ...

  5. 二十四种设计模式:中介者模式(Mediator Pattern)

    中介者模式(Mediator Pattern) 介绍用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 示例有一个Messa ...

  6. C#设计模式之十八中介者模式(Mediator Pattern)【行为型】

    一.引言 今天我们开始讲“行为型”设计模式的第五个模式,该模式是[中介者模式],英文名称是:Mediator Pattern.还是老套路,先从名字上来看看.“中介者模式”我第一次看到这个名称,我的理解 ...

  7. 设计模式的征途—22.中介者(Mediator)模式

    我们都用过QQ,它有两种聊天方式:一是私聊,二是群聊.使用QQ群,一个用户就可以向多个用户发送相同的信息和文件,从而无需一一发送,节省大量时间.通过引入群的机制,极大地减少系统中用户之间的两两通信,用 ...

  8. 【转】设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)

    设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据"单一职责原则",我们应该尽量将对象细化,使其只负责或呈现单一的职 ...

  9. 设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)

    设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据“单一职责原则”,我们应该尽量将对象细化,使其只负责或呈现单一的职责,即将行为分布到各 ...

随机推荐

  1. html-伪类

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. RBF:RBF基于近红外光谱的汽油辛烷值含量预测结果对比—Jason niu

    load spectra_data.mat temp = randperm(size(NIR,1)); P_train = NIR(temp(1:50),:)'; T_train = octane(t ...

  3. Airtest基本使用

    前段时间在博客中见到airtest的介绍,自己并实践了一番,用起来的确很方便,所以今天就来分享下. Airtest简介 Airtest是网易出品的一款基于图像识别和poco控件识别的一款UI自动化测试 ...

  4. RFC2616-HTTP1.1-Header Field Definitions(头字段规定部分—单词注释版)

    part of Hypertext Transfer Protocol -- HTTP/1.1RFC 2616 Fielding, et al. 14 Header Field Definitions ...

  5. html 知识点

    web服务本质 import socket def main(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind ...

  6. Git 使用问题记录

    问题一:新文件 add 后,提示有 modified 内容 描述:在 master 分支中增加了一个新的文件夹(-/cb4.6-zh),但执行 git add 后再查看状态却提示有 modified ...

  7. 校园网使用IPV6 tunnel免流量上网

    前段时间购买了一个vps,做梯子感觉不错,但是在校园网内,vps流量远超10块钱校园流量,眼看着上个月vps的流量被清零.但是校园网有免费的IPV6,而我的VPS也有个IPV6的地址,于是乎就想着如何 ...

  8. Stack [NOIP模拟] [组合数学经典]

    Description栈是常用的一种数据结构,有n个元素在栈顶端一侧等待进栈,栈顶端另一侧是出栈序列.你已经知道栈的操作有两种: push 和 pop ,前者是将一个元素进栈,后者是将栈顶元素弹出.现 ...

  9. Android Studio 设置字体

    File->Settings->Editor->Colors & Fonts->Font->Editor Font

  10. 学习Struts--Chap04:result中type属性dispatcher、redirect、redirectAction、chain的区别

    1.Struts2框架中常用的结果类型的分析和比较 dispatcher:缺省的result类型,type默认是dispatcher内部转发.如果不写type类型只写一个名字的话,不单是type类型默 ...