1 Star 0 Fork 2

星海/设计模式

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
中介者模式.txt 1.34 KB
一键复制 编辑 原始数据 按行查看 历史
Dick—_YangDi 提交于 2017-11-08 22:31 . fist all file
//中介者模式
#include<iostream>
#include<string>
using namespace std;
class Mediator;
class Base{
protected:
Mediator * m;
public:
void Set(Mediator * m);
virtual void Send(string Msg)=0;
virtual void Out(string Msg)=0;
};
class Mediator{
private:
Base * pb, * pg;
public:
void Set(Base * pb,Base * pg);
void Send(string Msg,Base * t);
};
class Boy:public Base{
public:
void Send(string Msg);
void Out(string Msg);
};
class Girl:public Base{
public:
void Send(string Msg);
void Out(string Msg);
};
//类内声明函数的实现
void Base::Set(Mediator * m){
this->m=m;
}
void Boy::Send(string Msg){
cout<<"男孩发出信息:";
m->Send(Msg,this);
}
void Girl::Send(string Msg){
cout<<"女孩发出信息:";
m->Send(Msg,this);
}
void Girl::Out(string Msg){
cout<<Msg<<endl;
cout<<"女孩收到信息"<<endl;
}
void Boy::Out(string Msg){
cout<<Msg<<endl;
cout<<"男孩收到信息"<<endl;
}
void Mediator::Set(Base * pb,Base * pg){
this->pb=pb;
this->pg=pg;
}
void Mediator::Send(string Msg,Base * t){
if(t!=pb){
pb->Out(Msg);
}else
pg->Out(Msg);
}
int main(void)
{
Boy * pb=new Boy();
Girl * pg=new Girl();
Mediator * pm=new Mediator();
pm->Set(pb,pg);
pb->Set(pm);
pg->Set(pm);
pb->Send("I love you");
pg->Send("You are a good man");
pb->Send("...");
delete pb;
delete pg;
delete pm;
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/qq_xinghai/SheJiMoShi.git
git@gitee.com:qq_xinghai/SheJiMoShi.git
qq_xinghai
SheJiMoShi
设计模式
master

搜索帮助