设计模式---中介者模式
来源:互联网 发布:2016真正的人工智能股 编辑:程序博客网 时间:2024/05/24 04:51
#include<iostream>
using namespace std;
class Country;
class UnitedNations
{
public:
virtual void Declare(string message,Country *colleague){}
};
class Country
{
protected:
UnitedNations *mediator;
public:
Country(UnitedNations *mediator)
{
this->mediator = mediator;
}
virtual void GetMessage(string mes){}
};
class USA:public Country
{
public:
USA(UnitedNations *mediator):Country(mediator){}
void CountryDeclare(string message)
{
mediator->Declare(message,this);
}
void GetMessage(string mes)
{
cout<<"i am usa : "<<mes<<endl;
}
};
class Iraq:public Country
{
public:
Iraq(UnitedNations *mediator):Country(mediator){}
void CountryDeclare(string message)
{
mediator->Declare(message,this);
}
void GetMessage(string mes)
{
cout<<"i am Iraq : "<<mes<<endl;
}
};
class UnitedNationsSecurityCouncil: public UnitedNations
{
private:
Country *coll1;
Country *coll2;
public:
void SetUSA(Country *col)
{
coll1 = col;
}
void SetIraq(Country *col)
{
coll2 = col;
}
void Declare(string mess,Country *coll)
{
if(coll == coll1)
{
coll2->GetMessage(mess);
}
else if (coll == coll2)
{
coll1->GetMessage(mess);
}
}
};
int main()
{
UnitedNationsSecurityCouncil *UNSC = new UnitedNationsSecurityCouncil();
USA *usa = new USA(UNSC);
Iraq *is = new Iraq(UNSC);
UNSC->SetUSA(usa);
UNSC->SetIraq(is);
usa->CountryDeclare("from usa iraq can not do it");
is->CountryDeclare("from iraq i can do that");
return 0;
}
- 【设计模式】中介者
- 中介者设计模式
- 设计模式-中介者
- 设计模式-中介者
- [设计]中介者模式
- 设计模式 中介者模式
- 设计模式-中介者模式
- 设计模式 - 中介者模式
- 设计模式-中介者模式
- 设计模式:中介者模式
- 设计模式--中介者模式
- 设计模式-中介者模式
- 设计模式--中介者模式
- 设计模式-中介者模式
- 设计模式-中介者模式
- 设计模式-中介者模式
- 【设计模式】中介者模式
- 设计模式:中介者模式
- C++ 对文件夹的安全权限操作
- 如何进入 Google 工作
- 【数据集介绍】ImageNet介绍
- mysql时间格式化处理
- MySQLERROR 1045 (28000)与ERROR 2002 (HY000)问题解决方法
- 设计模式---中介者模式
- windows蓝屏日志文件、dump文件收集方法
- 百度地图API之JavaScript 开源库1
- 生成迷宫的深度优先遍历算法的非递归实现
- Visual Assist 过期后重装不能使用的解决办法
- 微信小程序登录案例
- Spring中Bean的基于xml的三种实例化方式
- 百度地图API之JavaScript开源库2
- [转载]linux(x64)下安装Matlab 2015b破解版(含安装包)