Mediator中介者模式
做用:用一箇中介對象來封裝一系列的對象交互。中介者使各對象不須要顯式地相互引用,從而使其耦合鬆散,並且能夠獨立地改變它們之間的交互。ios
UML以下:this
Colleage抽象同事類,而ConcreteColleage是具體同時類,每一個具體同事只知道本身的行爲,而不瞭解其餘同事類的狀況,但它們卻都認識中介者對象,Mediator是抽象中介者,定義了同事對象到中介者對象的接口,ConcreteMediator是具體中介者對象,實現抽象類的方法,它須要知道全部具體同事類,並從具體同事接受消息,向具體同事對象發出命令。spa
Colleage類,抽象同事類設計
Mediator,抽象中介者類
說明:
1. Mediator 模式中,每一個Colleague 維護一個 Mediator,當要進行通訊時,每一個具體的 Colleague 直接向ConcreteMediator 發信息,至於信息發到哪裏,則由 ConcreteMediator 來決定。
2. ConcreteColleagueA 和 ConcreteColleagueB 沒必要維護對各自的引用,甚至它們也不知道各個的存在。
3. 優勢是,各個 Colleague 減小了耦合。
4. 缺點是,因爲 Mediator 控制了集中化,因而就把 Colleague 之間的交互複雜性變爲了中介者的複雜性,也就是中介者會變的比任何一個 Colleague 都複雜。
中介者模式很容易在系統中應用,也很容易在系統中誤用。當系統中出現了「多對多」交互複雜的對象羣時,不要急於使用中介者模式,而要先反思你的系統在設計上是否是合理。code
Mediator的出現減小了各個Colleage的耦合,使得能夠獨立地改變和複用各個Colleage類和Mediator;
因爲把對象如何協做進行了抽象,將中介做爲一個獨立的概念並將其封裝在一個對象中,這樣關注的對象就從對象各自自己的行爲轉移到它們之間的交互上來,也就是站在一個更宏觀的角度去看待系統。
因爲ConcreteMediator控制了集中化,因而就把交互複雜性變爲了中介者的複雜性,這使得中介者會變得比任何一個ConcreteColleage都複雜。對象
中介者模式的優勢來自集中控制,其缺點也是它。blog
中介者模式通常應用於一組對象以定義良好可是複雜的方式進行通訊的場合。接口
代碼以下:string
Mediator.hio
1 #ifndef _MEDIATOR_H_ 2 #define _MEDIATOR_H_ 3 4 #include <string> 5 6 using namespace std; 7 8 class Mediator; 9 10 class Colleage 11 { 12 public: 13 virtual ~Colleage(); 14 virtual void SetMediator(Mediator*); 15 virtual void SendMsg(string) = 0; 16 virtual void GetMsg(string) = 0; 17 protected: 18 Colleage(Mediator*); 19 Mediator* _mediator; 20 private: 21 22 }; 23 24 class ConcreteColleageA : public Colleage 25 { 26 public: 27 ~ConcreteColleageA(); 28 ConcreteColleageA(Mediator*); 29 virtual void SendMsg(string msg); 30 virtual void GetMsg(string); 31 protected: 32 private: 33 }; 34 35 class ConcreteColleageB : public Colleage 36 { 37 public: 38 ~ConcreteColleageB(); 39 ConcreteColleageB(Mediator*); 40 virtual void SendMsg(string msg); 41 virtual void GetMsg(string); 42 protected: 43 private: 44 }; 45 46 class Mediator 47 { 48 public: 49 virtual ~Mediator(); 50 virtual void SendMsg(string,Colleage*) = 0; 51 protected: 52 Mediator(); 53 private: 54 }; 55 56 class ConcreteMediator : public Mediator 57 { 58 public: 59 ConcreteMediator(); 60 ~ConcreteMediator(); 61 void SetColleageA(Colleage*); 62 void SetColleageB(Colleage*); 63 virtual void SendMsg(string msg,Colleage*); 64 protected: 65 private: 66 Colleage* m_ColleageA; 67 Colleage* m_ColleageB; 68 }; 69 #endif
Mediator.cpp
1 #include "Mediator.h" 2 #include <iostream> 3 #include <string> 4 5 using namespace std; 6 7 Colleage::Colleage(Mediator* pMediator) 8 { 9 this->_mediator = pMediator; 10 } 11 12 Colleage::~Colleage() 13 {} 14 15 void Colleage::SetMediator(Mediator* pMediator) 16 { 17 this->_mediator = pMediator; 18 } 19 20 ConcreteColleageA::ConcreteColleageA(Mediator* pMediator) : Colleage(pMediator) 21 { 22 } 23 24 ConcreteColleageA::~ConcreteColleageA() 25 { 26 } 27 28 void ConcreteColleageA::SendMsg(string msg) 29 { 30 this->_mediator->SendMsg(msg,this); 31 } 32 33 void ConcreteColleageA::GetMsg(string msg) 34 { 35 cout << "ConcreteColleageA Receive:"<< msg << endl; 36 } 37 38 ConcreteColleageB::ConcreteColleageB(Mediator* pMediator) : Colleage(pMediator) 39 { 40 } 41 42 ConcreteColleageB::~ConcreteColleageB() 43 { 44 } 45 46 void ConcreteColleageB::SendMsg(string msg) 47 { 48 this->_mediator->SendMsg(msg,this); 49 } 50 51 void ConcreteColleageB::GetMsg(string msg) 52 { 53 cout << "ConcreteColleageB Receive:" << msg << endl; 54 } 55 56 Mediator::Mediator() 57 {} 58 59 Mediator::~Mediator() 60 {} 61 62 ConcreteMediator::ConcreteMediator() 63 {} 64 65 ConcreteMediator::~ConcreteMediator() 66 {} 67 68 void ConcreteMediator::SetColleageA(Colleage* p) 69 { 70 this->m_ColleageA = p; 71 } 72 73 void ConcreteMediator::SetColleageB(Colleage* p) 74 { 75 this->m_ColleageB = p; 76 } 77 78 void ConcreteMediator::SendMsg(string msg,Colleage* p) 79 { 80 if(p == this->m_ColleageA) 81 { 82 this->m_ColleageB->GetMsg(msg); 83 } 84 else if(p == this->m_ColleageB) 85 { 86 this->m_ColleageA->GetMsg(msg); 87 } 88 }
main.cpp
1 #include "Mediator.h" 2 3 int main() 4 { 5 ConcreteMediator* pMediator = new ConcreteMediator(); 6 7 Colleage* p1 = new ConcreteColleageA(pMediator); 8 Colleage* p2 = new ConcreteColleageB(pMediator); 9 10 pMediator->SetColleageA(p1); 11 pMediator->SetColleageB(p2); 12 13 p1->SendMsg("xxx"); 14 p2->SendMsg("ooo"); 15 return 0; 16 }
結果以下: