中介者模式 C++ 实现
来源:互联网 发布:淘宝价钱修改会怎么样 编辑:程序博客网 时间:2024/06/04 18:08
#include<iostream>#include<string>#include<vector>#include<cstdlib>using namespace std;/* mediator 模式 问题:重在理解,结构图见网络。 */class colleage{ private: string name; string content; public: colleage(string n = " "):name(n) {} void set_name(string name) { this->name = name; } string get_name() { return this->name; } void set_content(string content) { this->content = content; } string get_content() { return this->content; } virtual void talk() { }};class monitor: public colleage{ public: monitor(string n = " "):colleage(n) { } void talk() { cout <<"monitor says:"<<get_content()<< endl; } };class tuanzishu: public colleage{ public: tuanzishu(string n = " "):colleage(n) { } void talk() { cout <<"tuanzishu say:"<<get_content()<< endl; } };class studentA: public colleage{ public: studentA(string n = " "):colleage(n) { } void talk() { cout <<"studentA say:"<< get_content()<< endl; } };class studentB: public colleage{ public: studentB(string n = " "):colleage(n) { } void talk() { cout <<"studentB say:"<<get_content()<< endl; } };class mediator{ public: vector<colleage*> studentlist; virtual void add_students(colleage *student) { studentlist.push_back(student); } };class QQmediator: public mediator{ public: virtual void notify(colleage *student) { student->talk(); for(int i = 0; i < studentlist.size(); i++) { if(student != studentlist[i]) { studentlist[i]->talk(); } } } virtual void chart(colleage *student1, colleage *student2) { student1->talk(); student2->talk(); } };int main(){//初始化 QQmediator qq; monitor studentmonitor("banzhang"); tuanzishu studenttuanzishu("tuanzishu"); studentA studentXM("xiaoming"); studentB studentXH("xiaohong"); //向中介者中添加同学 qq.add_students(&studentmonitor); qq.add_students(&studenttuanzishu); qq.add_students(&studentXM); qq.add_students(&studentXH); //设置各位同学的回复 studentmonitor.set_content("班会现在开始,请大家积极发言。"); studenttuanzishu.set_content("班会主题是XXXXX"); studentXM.set_content("我认为XXXXX"); studentXH.set_content("我不认为XXXX"); //班长通知后开始 cout <<"班长发布一个通知后的情景:"<< endl; qq.notify(&studentmonitor); //同学私聊 cout <<"同学私聊的情景:"<< endl; studentXM.set_content("你觉得今天的班会怎么样?"); studentXH.set_content("一般般吧,还是老样子,没解决什么问题"); qq.chart(&studentXM,&studentXH); system("pause"); return 0;}
总结: 无。详见参考:点击打开链接
- 【C++】Chapter21:中介者模式
- Objective C--中介者模式
- Objective C中介者模式
- Objective C--中介者模式
- C++实现中介者模式
- 中介者模式 C++ 实现
- 中介者模式lua实现
- 中介者模式----C++实现
- 中介者模式-C++实现
- js实现中介者模式
- 中介者模式(java实现)
- 设计模式 行为模式 中介者模式 c语言 版本实现
- 设计模式C++实现--中介者模式
- Objective-C中的中介者模式
- 中介者模式 Go语言实现
- 中介者模式(C++实现)
- 中介者模式的c++代码实现
- Java 实现中介者(Mediator)模式
- Design Pattern_Builder(建造者模式)
- 解决mysql“Access denied for user 'root'@'localhost'”
- 命名管道
- win8 硬盘版安装
- Android CTS环境搭建与测试方法
- 中介者模式 C++ 实现
- 斗地主的感悟(转)
- 易懂makefile教程
- VS2010 打开Form文件直接显示已编辑问题。
- seo影响因素有哪些
- ubuntu11.04编译运行Android2.3.源码
- opensuse 12.2 配置ldap客户端,启动悬挂,不能登陆
- A Christmas Carol——5、The end of the story
- 12.从零开始学习C语言--数组