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