设计模式之中介者模式(Mediator)
2012-08-30 18:40
666 查看
一、初识中介者模式
那些年,我们一起上过的大学,班级里有班长,有团书记。想一想如果没有QQ这种通讯工具的话,那么班长或者团支书该怎样下达消息呢??同时,班级上两个同学之间也可惜沟通啊,沟通一下,院里哪个女生,哪个帅哥呀~~~如果没有QQ的话,大概就是下面的情景:
View Code
下面是运行的结果:
看看,这样就利用qq这个平台完成了同学之间的交流。并且同学之间并没有相互联系,联系都是通过qq实现的,如果现在要对象进行扩展,比如也可以通过飞信啊,只需要加入class FeiXin:public Mediator 就可以了,如果想扩展同学C只需要,class StudentC:public Colleage 即可。并且在客户端的使用也是很方便的哦~~这时回过头想一想如果不用“中介者模式”的话,系统会是什么样呢???
[b]二、使用中介者模式的场合和优缺点[/b]
使用终结者模式的场合
1.一组定义良好的对象,现在要进行复杂的通信。
2.定制一个分布在多个类中的行为,而又不想生成太多的子类。
可以看出,中介对象主要是用来封装行为的,行为的参与者就是那些对象,但是通过中介者,这些对象不用相互知道。呵呵~~~
使用中介者模式的优点:
1.降低了系统对象之间的耦合性,使得对象易于独立的被复用。
2.提高系统的灵活性,使得系统易于扩展和维护。
使用中介者模式的缺点:
中介者模式的缺点是显而易见的,因为这个“中介“承担了较多的责任,所以一旦这个中介对象出现了问题,那么整个系统就会受到重大的影响。
那些年,我们一起上过的大学,班级里有班长,有团书记。想一想如果没有QQ这种通讯工具的话,那么班长或者团支书该怎样下达消息呢??同时,班级上两个同学之间也可惜沟通啊,沟通一下,院里哪个女生,哪个帅哥呀~~~如果没有QQ的话,大概就是下面的情景:
View Code
#include <iostream> #include <vector> #include <string> using namespace std; //抽象的同事类 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 content; } virtual void talk(){}; }; //具体的同事类:班长 class Monitor : public Colleage { public: Monitor(string n = ""):Colleage(n){}; virtual void talk() { cout<<"大班长 说:"<<get_content()<<endl; } }; //具体的同事类:团支书 class TuanZhiShu : public Colleage { public: TuanZhiShu(string n = ""):Colleage(n){}; virtual void talk() { cout<<"团支书 说:"<<get_content()<<endl; } }; //具体的同事类:同学A class StudentA : public Colleage { public: StudentA(string n = ""):Colleage(n){}; virtual void talk() { cout<<"学生 A 说:"<<get_content()<<endl; } }; //具体的同事类:同学B class StudentB : public Colleage { public: StudentB(string n = ""):Colleage(n){}; virtual void talk() { cout<<"学生 B 说:"<<get_content()<<endl; } }; //抽象中介者 class Mediator { public: vector<Colleage*> studentList; virtual void add_student(Colleage *student) { studentList.push_back(student); }; virtual void notify(Colleage *student){}; virtual void chart(Colleage *student1,Colleage *student2){}; }; //具体中介者qq通讯平台 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("Vincent"); TuanZhiShu studentTuanZhiShu("Robort"); StudentA studentA("Sam"); StudentB studentB("Tom"); /*----------------------班长发通知-----------------------------*/ cout<<"下面的班长发布一个通知的场景:"<<endl; //将同学们加入到qq群中 qq.add_student(&studentMonitor); qq.add_student(&studentTuanZhiShu); qq.add_student(&studentA); qq.add_student(&studentB); //设置大家的回复信息 studentMonitor.set_content("明天下午2点开年级会,收到回复^^。"); studentTuanZhiShu.set_content("知道了,肯定到!!"); studentA.set_content("收到了,但是可能晚点到!!"); studentB.set_content("收到了,但是明天要去面试!!"); //开始发通知 qq.notify(&studentMonitor); /*--------------------两个同学私下交流--------------------------*/ cout<<endl<<"下面是两个同学的私下交流:"<<endl; studentMonitor.set_content("你觉得咱们“软件项目管理老师”讲的怎么样?"); studentA.set_content("我觉得讲的不够生动,还点名,不太好!!!"); qq.chart(&studentMonitor,&studentA); return 0; }
下面是运行的结果:
看看,这样就利用qq这个平台完成了同学之间的交流。并且同学之间并没有相互联系,联系都是通过qq实现的,如果现在要对象进行扩展,比如也可以通过飞信啊,只需要加入class FeiXin:public Mediator 就可以了,如果想扩展同学C只需要,class StudentC:public Colleage 即可。并且在客户端的使用也是很方便的哦~~这时回过头想一想如果不用“中介者模式”的话,系统会是什么样呢???
[b]二、使用中介者模式的场合和优缺点[/b]
使用终结者模式的场合
1.一组定义良好的对象,现在要进行复杂的通信。
2.定制一个分布在多个类中的行为,而又不想生成太多的子类。
可以看出,中介对象主要是用来封装行为的,行为的参与者就是那些对象,但是通过中介者,这些对象不用相互知道。呵呵~~~
使用中介者模式的优点:
1.降低了系统对象之间的耦合性,使得对象易于独立的被复用。
2.提高系统的灵活性,使得系统易于扩展和维护。
使用中介者模式的缺点:
中介者模式的缺点是显而易见的,因为这个“中介“承担了较多的责任,所以一旦这个中介对象出现了问题,那么整个系统就会受到重大的影响。
相关文章推荐
- 设计模式(十九)之Mediator(中介者)
- 设计模式之中介者模式(Mediator)
- 设计模式:中介者模式-mediator
- 设计模式之(十九)中介者模式Mediator
- c++设计模式-----中介者模式Mediator
- C#面向对象模式设计第十七讲:Mediator 中介者模式(行为型模式)
- GOF23设计模式之 中介者模式Mediator
- 设计模式:中介者模式(Mediator)
- 设计模式之Mediator(中介者)
- 设计模式-行为设计模式:中介者模式 Mediator
- 24种设计模式--中介者模式【Mediator Pattern】
- 23种设计模式之中介者模式(Mediator)
- 【设计模式】—— 中介者模式Mediator
- 设计模式之Mediator(中介者)
- 我所理解的设计模式(C++实现)——中介者模式(Mediator Pattern)
- 设计模式(21)-Mediator 中介者模式
- 设计模式 -- 中介者设计模式 (Mediator Pattern)
- 设计模式之二十一:中介者模式(Mediator)
- 如何让孩子爱上设计模式 ——19.中介者模式(Mediator Pattern)
- 设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)