軟件設計 -- 中介者模式

概述:

    假設我們開發一個圖片處理軟件,裏面肯定包括很多相關功能,比如說剪切,旋轉,濾鏡,美化等等,而我們這些功能所要處理的對象是固定的,就是我們所顯示的那張圖片。但是我們不能把所有的功能羅列到一個tab上,雖然這樣處理方便但是不美觀。這是我們可以這樣子:用一箇中介者類負責所有功能的初始化和具體執行,我們需要功能時直接調用中介者類即可。

    中介者模式就是定義一箇中介對象來封裝系列對象之間的交互。中介者使各個對象不需要顯示地相互引用,從而使其耦合性鬆散,而且可以獨立地改變他們之間的交互。

類圖和實例


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

ConcreteMediator:具體中介者對象,實現抽象類中的方法,此具體中介者對象需要知道所有具體同事類,並從具體同事接受消息,向具體同事對象發送命令。

Colleague類:抽象同事類。

ConcreteColleague類:具體同事類,實現抽象同事類中的方法。每一個同時類需要知道中介者對象;每個具體同事類只需要瞭解自己的行爲,而不需要了解其他同事類的情況。

[cpp] view plain copy
  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. //  中介類
  77. class Mediator  
  78. {  
  79. public:  
  80.     vector<Colleage*> studentList;  
  81.     virtual void add_student(Colleage *student)  
  82.     {  
  83.         studentList.push_back(student);  
  84.     };  
  85.     virtual void notify(Colleage *student){};      
  86. };  
  87.   
  88. class QQMediator : public Mediator  
  89. {  
  90. public:  
  91.     virtual void notify(Colleage *student)  
  92.     {  
  93.         student->talk();  
  94.         for(int i = 0 ; i < studentList.size() ; ++i)  
  95.         {              
  96.             if(student != studentList[i])  
  97.             {  
  98.                 studentList[i]->talk();  
  99.             }  
  100.         }  
  101.     };    
  102. };  
  103.   
  104.   
  105. int main()  
  106. {  
  107.     QQMediator qq;  
  108.     Monitor *studentMonitor = new Monitor("Foxx");  
  109.     Secretary *studentSecretary = new Secretary("TC");  
  110.     StudentA *studentA = new StudentA("Jack");  
  111.     StudentB *studentB = new StudentB("Frank");          
  112.   
  113.     qq.add_student(studentSecretary);  
  114.     qq.add_student(studentA);  
  115.     qq.add_student(studentB);       
  116.   
  117.     studentMonitor->set_content("明天開始放假!");  
  118.     qq.notify(studentMonitor);     
  119.     return 0;  
  120. }  



適用性:

1.一組對象以定義良好但是複雜的方式進行通信。產生的相互依賴關係結構混亂且難以理解。

2.一個對象引用其他很多對象並且直接與這些對象通信,導致難以複用該對象。

3.想定製一個分佈在多個類中的行爲,而又不想生成太多的子類。

優缺點:

使用中介者模式的優點:

  1.降低了系統對象之間的耦合性,使得對象易於獨立的被複用。

  2.提高系統的靈活性,使得系統易於擴展和維護。

使用中介者模式的缺點:

  由於我們這個中介承擔了較多的責任,所以一旦這個中介對象出現了問題,那麼整個系統就會受到重大的影響。

發佈了202 篇原創文章 · 獲贊 119 · 訪問量 45萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章