#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; }
总结: 无。详见参考:点击打开链接
原文链接: https://www.cnblogs.com/dyllove98/p/3149480.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/93032
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!