简述:
中介者模式:用一个中介对象来封装一系列的对象交互。中介者使各个对象不需要显示地相互引用,从而使其耦合松散,而且可以独立地改变他们之间的交互。
中介者模式包括:抽象中介者类、具体中介者类、抽象同事类、具体同事类。
抽象中介者类:定义了同事对象到中介者对象的接口。
具体中介者类:实现抽象中介者类的方法,它需要知道所有具体同事类,并从具体同事接收消息,向具体同事对象发出命令。
抽象同事类:抽象接口。
具体同事类:每个具体同事只知道自己的行为,而不连接其他同事类的情况,但它们缺都认识中介者对象。
中介者模式:
1 #include <iostream> 2 using namespace std; 3 4 class CMediator; 5 //抽象同事类 6 class CColleague 7 { 8 protected: 9 CMediator* m_pMediator; 10 11 public: 12 CColleague(CMediator* pMediator) { m_pMediator = pMediator; } 13 }; 14 15 // 抽象中介者类 16 class CMediator 17 { 18 public: 19 virtual void Send(string szMessage, CColleague* pColleague) = 0; 20 }; 21 22 // 具体同事类 23 class CConcreteColleague1 : public CColleague 24 { 25 public: 26 CConcreteColleague1(CMediator* pMediator) : CColleague(pMediator) {} 27 28 void Send(string szMessage) { m_pMediator->Send(szMessage, this); } 29 30 void Notify(string szMessage) { cout << "同事1得到消息" << szMessage << endl; } 31 }; 32 33 // 具体同事类 34 class CConcreteColleague2 : public CColleague 35 { 36 public: 37 CConcreteColleague2(CMediator* pMediator) : CColleague(pMediator) {} 38 39 void Send(string szMessage) { m_pMediator->Send(szMessage, this); } 40 41 void Notify(string szMessage) { cout << "同事2得到消息" << szMessage << endl; } 42 }; 43 44 // 具体中介者类 45 class CConcreteMediator : public CMediator 46 { 47 private: 48 CConcreteColleague1* m_pColleague1; 49 CConcreteColleague2* m_pColleague2; 50 51 public: 52 void SetColleague1(CConcreteColleague1* pColleague) { m_pColleague1 = pColleague; } 53 54 void SetColleague2(CConcreteColleague2* pColleague) { m_pColleague2 = pColleague; } 55 56 virtual void Send(string szMessage, CColleague* pColleague) 57 { 58 if (pColleague == m_pColleague1) 59 m_pColleague2->Notify(szMessage); 60 else 61 m_pColleague1->Notify(szMessage); 62 } 63 }; 64 65 int main() 66 { 67 CConcreteMediator Mediator; 68 CConcreteColleague1 Colleague1(&Mediator); 69 CConcreteColleague2 Colleague2(&Mediator); 70 71 Mediator.SetColleague1(&Colleague1); 72 Mediator.SetColleague2(&Colleague2); 73 74 Colleague1.Send("吃过饭了吗?"); 75 Colleague2.Send("没有,你打算请客?"); 76 77 system("pause"); 78 return 0; 79 }
输出结果:
例:联合国打嘴仗
代码如下:
1 #include <iostream> 2 using namespace std; 3 4 class CUniteNations; 5 // 国家(抽象同事类) 6 class CCountry 7 { 8 protected: 9 CUniteNations* m_pMediator; 10 11 public: 12 CCountry(CUniteNations* pMediator) { m_pMediator = pMediator; } 13 }; 14 15 // 联合国(抽象中介者类) 16 class CUniteNations 17 { 18 public: 19 // 声明 20 virtual void Declare(string szMessage, CCountry* pColleague) = 0; 21 }; 22 23 // 中国(具体同事类) 24 class CChina: public CCountry 25 { 26 public: 27 CChina(CUniteNations* pMediator) : CCountry(pMediator) {} 28 29 // 声明 30 void Declare(string szMessage) { m_pMediator->Declare(szMessage, this); } 31 32 // 获得消息 33 void GetMessage(string szMessage) { cout << "*获得对方消息:" << szMessage << endl; } 34 }; 35 36 // 美国(具体同事类) 37 class CUSA : public CCountry 38 { 39 public: 40 CUSA(CUniteNations* pMediator) : CCountry(pMediator) {} 41 42 // 声明 43 void Declare(string szMessage) { m_pMediator->Declare(szMessage, this); } 44 45 // 获得消息 46 void GetMessage(string szMessage) { cout << "万恶的美帝获得对方消息:" << szMessage << endl; } 47 }; 48 49 // 联合国安全理事会(具体中介者类) 50 class CUniteNationsSecurityCouncil : public CUniteNations 51 { 52 private: 53 CChina* m_pColleague1; 54 CUSA* m_pColleague2; 55 56 public: 57 void SetColleague1(CChina* pColleague1) { m_pColleague1 = pColleague1; } 58 59 void SetColleague2(CUSA* pColleague2) { m_pColleague2 = pColleague2; } 60 61 void Declare(string szMessage, CCountry* pColleague) 62 { 63 if (pColleague == m_pColleague1) 64 m_pColleague2->GetMessage(szMessage); 65 else 66 m_pColleague1->GetMessage(szMessage); 67 } 68 }; 69 70 71 int main() 72 { 73 CUniteNationsSecurityCouncil UNSC; 74 CChina China(&UNSC); 75 CUSA USA(&UNSC); 76 77 UNSC.SetColleague1(&China); 78 UNSC.SetColleague2(&USA); 79 80 USA.Declare("我们要经济制裁你!"); 81 China.Declare("你们特靠谱是逗比~"); 82 83 USA.Declare("我们要科技制裁你!"); 84 China.Declare("你们特靠谱是逗比~"); 85 86 USA.Declare("我们要全球孤立你!"); 87 China.Declare("你们特靠谱是逗比~"); 88 89 USA.Declare("咱们能不提老特的事么?"); 90 China.Declare("你们黑白配玩的挺嗨~"); 91 China.Declare("你们疫情控制得好~"); 92 China.Declare("你们有小弟要搞独立~"); 93 94 USA.Declare("咱还是说老特的事吧。。。"); 95 96 system("pause"); 97 return 0; 98 }
输出结果: