设计模式之二十一:中介者模式(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.概述 在面向对象的软件设计与开发过程中,根据“单一职责原则”,我们应该尽量将对象细化,使其只负责或呈现单一的职责,即将行为分布到各 ...
随机推荐
- HDFS格式化namenode后启动集群datanode不启动
这是因为格式化namenode之后,namenode节点中VERSION的ClusterID会改变!!! 解决办法: 1.打开namenode节点的hdfs-site.xml文件,查看你的nameno ...
- Booksort POJ - 3460 (IDA*)
Description The Leiden University Library has millions of books. When a student wants to borrow a ce ...
- To the Max POJ - 1050 (最大子段和)
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous s ...
- java、python与留下迷点的php hash collision
JAVA 生成java的碰撞数据比较简单 根据网上资料可知: at,bU,c6的在java中的hash值是相同的 则可以根据这三个不断做 笛卡尔积 简单明了就是做字符串拼接. 举个例子 把A当做at, ...
- 持续集成时 travis 和 codecov 等 yaml 文件的配置
最近在项目中在配置CodeCov 以及Travis 和 AppVeyor做持续集成时,遇到了一些问题,也解决了一些问题.顺便拿来分享一下. 首先时Travis,这个主要是来跑基于 Linux 环境下的 ...
- Unity容器的简单AOP与DI的应用Demo(基于asp.net mvc框架)
转发请注明出处:https://home.cnblogs.com/u/zhiyong-ITNote/ 整个Demo是基于Controller-Service-Repository架构设计的,每一层之间 ...
- django-debug-toolbar和Django 日志配置
django-debug-toolbar介绍 django-debug-toolbar 是一组可配置的面板,可显示有关当前请求/响应的各种调试信息,并在单击时显示有关面板内容的更多详细信息. gith ...
- Java笔记(一)编程基础与二进制
编程基础与二进制 一.编程基础 函数调用的基本原理: 函数调用中的问题: 1)参数如何传递? 2)函数如何知道返回什么地方? 3)函数结果如何传递给调用方? 解决思路是使用内存来函数调用过程中需要的数 ...
- Scrapy基础(五) ------css选择器基础
基本语法: * 选择所有节点#container 选择id为container的节点.container 选择所有class包含contai ...
- docker自动重启容器
docker run --restart=always -d --name myunbuntu ubuntu /bin/bash -c "l am a docker" //无 ...