1. 概述
Mediator Pattern:封装了 一组对象之间的交互,使得对象之间保持松耦合的关系,避免对象维护复杂的关系交互。
简单的说,Mediator封装的是交互。
类图:
object实例之间的交互,统一由Mediator维护。
2. 实例
餐馆有cook和waiter,cook之间需要彼此交流配合,也需要和waiter进行交流, 例如一个厨师请另一个厨师帮忙切菜,厨师请waiter将菜送给客人。
人多了,沟通就不顺畅了,为此,配一个协调员,彼此之间的交互,统一由协调员传达。
代码:
输出:
zhang san request to li si : qie cai
li si receive request from zhang san : qie cai
zhang san request to cui hua : shang shuan cai
cui hua receive request from zhang san : shang shuan cai
Mediator Pattern:封装了 一组对象之间的交互,使得对象之间保持松耦合的关系,避免对象维护复杂的关系交互。
简单的说,Mediator封装的是交互。
类图:
![](http://hi.csdn.net/attachment/201202/24/0_1330074466C5t1.gif)
2. 实例
餐馆有cook和waiter,cook之间需要彼此交流配合,也需要和waiter进行交流, 例如一个厨师请另一个厨师帮忙切菜,厨师请waiter将菜送给客人。
人多了,沟通就不顺畅了,为此,配一个协调员,彼此之间的交互,统一由协调员传达。
代码:
#include <iostream>
#include <string>
#include <map>
using namespace std;
class Mediator;
//object
class Employee
{
public:
virtual ~Employee()
{
}
virtual void send(string, string) = 0;
virtual void receive(string, string) = 0;
protected:
string _name;
Mediator* _mediator;
};
//Mediator
class Mediator
{
public:
void add(string name, Employee* _emp)
{
_map_name_employee.insert(pair<string, Employee*>(name, _emp));
}
void send(string name_from, string name_to, string context)
{
map<string, Employee*>::iterator iter;
iter = _map_name_employee.find(name_to);
if(_map_name_employee.end() == iter)
{
cout << name_to.c_str() << " has resigned!" << endl;
return;
}
else
{
//cout << _name << "\trequest to" << name.c_str() << " : " << context.c_str() << endl;
(iter->second)->receive(name_from, context);
}
}
private:
map<string, Employee*> _map_name_employee;
};
//concrete_object_1
class Cook : public Employee
{
public:
Cook(char*name, Mediator *mediator)
{
_name = name;
_mediator = mediator;
_mediator->add(_name, this);
}
void send(string name, string context)
{
cout << _name << " request to " << name.c_str() << " : " << context.c_str() << endl;
_mediator->send(_name, name, context);
}
void receive(string name, string context)
{
cout << _name << "\treceive request from " << name.c_str() << " : " << context.c_str() << endl;
}
};
//concrete_object_2
class Waiter : public Employee
{
public:
Waiter(char* name, Mediator *mediator)
{
_name = name;
_mediator = mediator;
_mediator->add(_name, this);
}
void send(string name, string context)
{
cout << _name << " request to " << name.c_str() << " : " << context.c_str() << endl;
_mediator->send(_name, name, context);
}
void receive(string name, string context)
{
cout << _name << "\treceive request from " << name.c_str() << " : " << context.c_str() << endl;
}
};
//client
int main()
{
Mediator mediator;
//object初始化
Cook c1("zhang san", &mediator);
Cook c2("li si", &mediator);
Waiter w1("cui hua", &mediator);
Waiter w2("xiao cui", &mediator);
//交互
c1.send("li si", "qie cai");
c1.send("cui hua", "shang shuan cai");
return 0;
}
输出:
zhang san request to li si : qie cai
li si receive request from zhang san : qie cai
zhang san request to cui hua : shang shuan cai
cui hua receive request from zhang san : shang shuan cai