(C++实现)——中介者模式(Mediator Pattern)

来源:互联网 发布:淘宝店铺改类别 编辑:程序博客网 时间:2024/05/01 01:45

概述:

    假设我们开发一个图片处理软件,里面肯定包括很多相关功能,比如说剪切,旋转,滤镜,美化等等,而我们这些功能所要处理的对象是固定的,就是我们所显示的那张图片。但是我们不能把所有的功能罗列到一个tab上,虽然这样处理方便但是不美观。这是我们可以这样子:用一个中介者类负责所有功能的初始化和具体执行,我们需要功能时直接调用中介者类即可。

    中介者模式就是定义一个中介对象来封装系列对象之间的交互。中介者使各个对象不需要显示地相互引用,从而使其耦合性松散,而且可以独立地改变他们之间的交互。

类图和实例


Mediator:抽象中介者,定义了同事对象交互的接口。

ConcreteMediator:具体中介者对象,实现抽象类中的方法,此具体中介者对象需要知道所有具体同事类,并从具体同事接受消息,向具体同事对象发送命令。

Colleague类:抽象同事类。

ConcreteColleague类:具体同事类,实现抽象同事类中的方法。每一个同时类需要知道中介者对象;每个具体同事类只需要了解自己的行为,而不需要了解其他同事类的情况。

[cpp] view plaincopy
  1. #include <iostream>  
  2. #include <vector>  
  3. #include <string>  
  4.   
  5. using namespace std;  
  6.   
  7. class Colleage  
  8. {  
  9. private:  
  10.     string name;  
  11.     string content;  
  12. public:  
  13.     Colleage(string n = " "):name(n){};  
  14.     void set_name(string name)  
  15.     {  
  16.         this->name = name;  
  17.     }  
  18.     string get_name()  
  19.     {  
  20.         return this->name;  
  21.     }  
  22.     void set_content(string content)  
  23.     {  
  24.         this->content = content;  
  25.     }  
  26.     string get_content()  
  27.     {  
  28.         if(content.size() != 0)  
  29.             return content;  
  30.         else return "Copy that";  
  31.     }  
  32.     virtual void talk(){};  
  33.   
  34. };  
  35.   
  36. class Monitor : public Colleage  
  37. {  
  38. public:  
  39.     Monitor(string n = ""):Colleage(n){};  
  40.     virtual void talk()  
  41.     {  
  42.         cout<<"班长 "<<get_name()<<" 说:"<<get_content()<<endl;  
  43.     }  
  44. };  
  45.   
  46. class Secretary : public Colleage  
  47. {  
  48. public:  
  49.     Secretary(string n = ""):Colleage(n){};  
  50.     virtual void talk()  
  51.     {  
  52.         cout<<"团支书 "<<get_name()<<" 说:"<<get_content()<<endl;  
  53.     }  
  54. };  
  55.   
  56. class StudentA : public Colleage  
  57. {  
  58. public:  
  59.     StudentA(string n = ""):Colleage(n){};  
  60.     virtual void talk()  
  61.     {  
  62.         cout<<"学生 A "<<get_name()<<" 说:"<<get_content()<<endl;  
  63.     }  
  64. };  
  65.   
  66. class StudentB : public Colleage  
  67. {  
  68. public:  
  69.     StudentB(string n = ""):Colleage(n){};  
  70.     virtual void talk()  
  71.     {  
  72.         cout<<"学生 B "<<get_name()<<" 说:"<<get_content()<<endl;  
  73.     }  
  74. };  
  75.   
  76. class Mediator  
  77. {  
  78. public:  
  79.     vector<Colleage*> studentList;  
  80.     virtual void add_student(Colleage *student)  
  81.     {  
  82.         studentList.push_back(student);  
  83.     };  
  84.     virtual void notify(Colleage *student){};      
  85. };  
  86.   
  87. class QQMediator : public Mediator  
  88. {  
  89. public:  
  90.     virtual void notify(Colleage *student)  
  91.     {  
  92.         student->talk();  
  93.         for(int i = 0 ; i < studentList.size() ; ++i)  
  94.         {              
  95.             if(student != studentList[i])  
  96.             {  
  97.                 studentList[i]->talk();  
  98.             }  
  99.         }  
  100.     };    
  101. };  
  102.   
  103.   
  104. int main()  
  105. {  
  106.     QQMediator qq;  
  107.     Monitor *studentMonitor = new Monitor("Foxx");  
  108.     Secretary *studentSecretary = new Secretary("TC");  
  109.     StudentA *studentA = new StudentA("Jack");  
  110.     StudentB *studentB = new StudentB("Frank");          
  111.   
  112.     qq.add_student(studentSecretary);  
  113.     qq.add_student(studentA);  
  114.     qq.add_student(studentB);       
  115.   
  116.     studentMonitor->set_content("明天开始放假!");  
  117.     qq.notify(studentMonitor);     
  118.     return 0;  
  119. }  

适用性:

1.一组对象以定义良好但是复杂的方式进行通信。产生的相互依赖关系结构混乱且难以理解。

2.一个对象引用其他很多对象并且直接与这些对象通信,导致难以复用该对象。

3.想定制一个分布在多个类中的行为,而又不想生成太多的子类。

优缺点:

使用中介者模式的优点:

  1.降低了系统对象之间的耦合性,使得对象易于独立的被复用。

  2.提高系统的灵活性,使得系统易于扩展和维护。

使用中介者模式的缺点:

  由于我们这个中介承担了较多的责任,所以一旦这个中介对象出现了问题,那么整个系统就会受到重大的影响。


LCL_data原创于CSDN.NET【http://blog.csdn.net/lcl_data/article/details/9529427】

其他设计模式请参看:我所理解的设计模式

原创粉丝点击