部分内容参考大话设计模式第25章;本实验通过C++语言实现。
一 原理
意图:用一个中介对象来封装一系列对象的交互,中介者使得各个对象不需要显示地相互引用,从而使耦合松散,而且可以独立地改变它们之间的交互。
上下文:多个对象互相关联交互,对象之间维持一种复杂的网状引用关系。如果遇到一些需求更改,这种直接的引用关系将面临不断的变化。
如何管理对象间的复杂关联关系,避免相互交互的对象之间的紧耦合引用关系,从而更好地抵御变化?
中介者模式静态类图
Mediator:定义一个接口与各个Colleague(同事)通信。
ConcreteMediator:通过协调各个Colleague实现协作行为,了解并维护它的各个Colleague。
Colleague:每个Colleague都知道它的Mediator对象;每个Colleague在需要与其他Colleague通信的时候,都与它的Mediator通信。
测试代码
#include <iostream>
#include <list>
#include <mutex>
#include <inttypes.h>
using namespace std;
class Mediator;
class Colleague
{
public:
Mediator *mediator;
Colleague(Mediator *);
virtual void send(string msg) = 0;
virtual void notify(string msg) = 0;
};
class Mediator
{
public:
virtual void send(Colleague *c,string msg) = 0;
};
Colleague::Colleague(Mediator *m):mediator(m)
{
}
class ConcreteColleague1:public Colleague
{
public:
ConcreteColleague1(Mediator *m):Colleague(m){
}
// Colleague interface
public:
void send(string msg) override
{
this->mediator->send(this,msg);
}
void notify(string msg) override{
cout << "同事1得到信息:" << msg << endl;
}
};
class ConcreteColleague2:public Colleague
{
public:
ConcreteColleague2(Mediator *m):Colleague(m){
}
// Colleague interface
public:
void send(string msg) override
{
this->mediator->send(this,msg);
}
void notify(string msg) override{
cout << "同事2得到信息:" << msg << endl;
}
};
class ConreteMediator:public Mediator
{
public:
Colleague *c1;
Colleague *c2;
void setC1(Colleague *value);
void setC2(Colleague *value);
// Mediator interface
public:
void send(Colleague *c, string msg)
{
if(c == c1){
c2->notify(msg);
}else{
c1->notify(msg);
}
}
};
void ConreteMediator::setC2(Colleague *value)
{
c2 = value;
}
void ConreteMediator::setC1(Colleague *value)
{
c1 = value;
}
int main(void)
{
ConreteMediator *m = new ConreteMediator();
Colleague *c1 = new ConcreteColleague1(m);
Colleague *c2 = new ConcreteColleague2(m);
m->setC1(c1);
m->setC2(c2);
c1->send("我是C1");
c2->send("我是C2");
return 0;
}
测试结果:
同事2得到信息:我是C1
同事1得到信息:我是C2
二 实例
抽象后的UML图
测试代码:
#include <iostream>
#include <list>
#include <mutex>
#include <inttypes.h>
using namespace std;
class guojia;
//机构
class jigou{
public:
//传话
virtual void chuanhua(string msg,guojia *g) = 0;
};
//国家
class guojia{
public:
jigou *mediator;
guojia(jigou *j):mediator(j){
}
virtual void read(string msg) = 0;
virtual void write(string msg) = 0;
};
//美国
class meiguo:public guojia{
public:
meiguo(jigou *j):guojia(j){}
// guojia interface
public:
void read(string msg)
{
cout << "==>【美国收到消息】 " << msg << endl;
}
void write(string msg)
{
cout << "【美国发消息】 " << msg << endl;
this->mediator->chuanhua(msg,this);
}
};
//伊拉克
class yilake:public guojia{
public:
yilake(jigou *j):guojia(j){}
// guojia interface
public:
void read(string msg)
{
cout << "==>【伊拉克收到消息】 " << msg << endl;
}
void write(string msg)
{
cout << "【伊拉克发消息】 " << msg << endl;
this->mediator->chuanhua(msg,this);
}
};
class anlihui :public jigou{
public:
meiguo *g1;
yilake *g2;
// jigou interface
public:
void chuanhua(string msg,guojia *g)
{
if(g == g1){
g2->read(msg);
}else{
g1->read(msg);
}
}
void setG1(meiguo *value);
void setG2(yilake *value);
};
void anlihui::setG2(yilake *value)
{
g2 = value;
}
void anlihui::setG1(meiguo *value)
{
g1 = value;
}
int main(void)
{
anlihui *m = new anlihui();
meiguo *g1 = new meiguo(m);
yilake *g2 = new yilake(m);
m->setG1(g1);
m->setG2(g2);
g1->write("地球是方的,跟我念");
g2->write("地球是芳芳的");
g1->write("芳芳是谁?");
g2->write("没有芳芳");
g1->write("交出芳芳,保你荣华富贵");
g2->write("真没有芳芳");
return 0;
}
运行结果:
【美国发消息】 地球是方的,跟我念
==>【伊拉克收到消息】 地球是方的,跟我念
【伊拉克发消息】 地球是芳芳的
==>【美国收到消息】 地球是芳芳的
【美国发消息】 芳芳是谁?
==>【伊拉克收到消息】 芳芳是谁?
【伊拉克发消息】 没有芳芳
==>【美国收到消息】 没有芳芳
【美国发消息】 交出芳芳,保你荣华富贵
==>【伊拉克收到消息】 交出芳芳,保你荣华富贵
【伊拉克发消息】 真没有芳芳
==>【美国收到消息】 真没有芳芳