设计模式C++描述----18.中介者(Mediator)模式
一. 举例
比如,现在中图和日本在关于易做图问题上存在争端。这时,联合国就会站出来,做为调解者,其实也没什么好调解的,易做图本来就是中国的,这是不争的事实!联合国也就是个传话者、发言人。
结构图如下:
代码如下:
[cpp] //抽象国家
class Country
{
protected:
Mediator *m_mediator; //中介
public:
virtual void SetMediator(Mediator *mediator){} //设置中介
virtual void SendMessage(string message) {} //向中介发信息
virtual void GetMessage(string message) {} //从中介获取信息
};
//抽象中介者
class Mediator
{
public:
virtual void Send(string message, Country *person) {}
virtual void SetJanpa(Country *Janpa) {} //让中介认识具体的对象
virtual void SetChina(Country *China) {}
};
//日本
class Janpa: public Country
{
public:
void SetMediator(Mediator *mediator) { m_mediator = mediator; }
void SendMessage(string message) { m_mediator->Send(message, this); }
void GetMessage(string message) { cout<<"日本收到信息: "<<message; }
};
//中国
class China: public Country
{
public:
void SetMediator(Mediator *mediator) { m_mediator = mediator; }
void SendMessage(string message) { m_mediator->Send(message, this); }
void GetMessage(string message) { cout<<"中国收到信息: "<<message; }
};
//联合国
class UN : public Mediator
{
private:
Country *m_Janpa; //日本
Country *m_China; //中国
public:
UN(): m_Janpa(NULL), m_China(NULL) {}
void SetJanpa(Country *Janpa) { m_Janpa = Janpa; }
void SetChina(Country *China) { m_China = China; }
void Send(string message, Country *country)
{
if(country == m_Janpa) //日本给中图发信息
m_China->GetMessage(message); //中国收到信息
else
m_Janpa->GetMessage(message);
}
};
//测试代码
int main()
{
Mediator *mediator = new UN();
Country *pJanpa = new Janpa(); //日本
Country *pChina = new China(); //中国
mediator->SetJanpa(Janpa);
mediator->SetChina(China);
pJanpa->SetMediator(mediator);
pChina->SetMediator(mediator);
pJanpa->SendMessage("易做图是我们的,哈哈哈...\n");
pChina->SendMessage("操,易做图再乱放易做图,小心我弄死你!\n");
delete pJanpa;
delete pChina;
delete mediator;
return 0;
}
//抽象国家
class Country
{
protected:
Mediator *m_mediator; //中介
public:
virtual void SetMediator(Mediator *mediator){} //设置中介
virtual void SendMessage(string message) {} //向中介发信息
virtual void GetMessage(string message) {} //从中介获取信息
};
//抽象中介者
class Mediator
{
public:
virtual void Send(string message, Country *person) {}
virtual void SetJanpa(Country *Janpa) {} //让中介认识具体的对象
virtual void SetChina(Country *China) {}
};
//日本
class Janpa: public Country
{
public:
void SetMediator(Mediator *mediator) { m_mediator = mediator; }
void SendMessage(string message) { m_mediator->Send(message, this); }
void GetMessage(string message) { cout<<"日本收到信息: "<<message; }
};
//中国
class China: public Country
{
public:
void SetMediator(Mediator *mediator) { m_mediator = mediator; }
void SendMessage(string message) { m_mediator->Send(message, this); }
void GetMessage(string message) { cout<<"中国收到信息: "<<message; }
};
//联合国
class UN : public Mediator
{
private:
Country *m_Janpa; //日本
Country *m_China; //中国
public:
UN(): m_Janpa(NULL), m_China(NULL) {}
void SetJanpa(Country *Janpa) { m_Janpa = Janpa; }
void SetChina(Country *China) { m_China = China; }
void Send(string message, Country *country)
{
if(country == m_Janpa) //日本给中图发信息
m_China->GetMessage(message); //中国收到信息
else
m_Janpa->GetMessage(message);
}
};
//测试代码
int main()
{
Mediator *mediator = new UN();
Country *pJanpa = new Janpa(); //日本
Country *pChina = new China(); //中国
mediator->SetJanpa(Janpa);
mediator->SetChina(China);
pJanpa->SetMediator(mediator);
pChina->SetMediator(mediator);
pJanpa->SendMessage("易做图是我们的,哈哈哈...\n");
pChina->SendMessage("操,易做图再乱放易做图,小心我弄死你!\n");
delete pJanpa;
delete pChina;
delete mediator;
return 0;
}
二. 中介者模式
定义:用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
补充:软件开发 , C++ ,