1#include <iostream>2#include <string>3 4 using namespacestd;5 6 classcolleague;7 8 classMediator9 {Ten Public: One Virtual voidSend (stringstrmessage, colleague* pstcolleague) =0; A }; - - classcolleague the { - Public: -Colleague (mediator*pstmediator): M_pstmediator (pstmediator) - { + - } + A Virtual voidSend (stringStrmes) =0; at - Virtual voidNotify (stringStrmes) =0; - - protected: -mediator*M_pstmediator; - }; in - classConcretecolleaguea: Publiccolleague to { + Public: -Concretecolleaguea (mediator*Pstmediator): Colleague (Pstmediator) the { * $ }Panax Notoginseng - Virtual voidSend (stringSTRMSG) the { +M_pstmediator->send (STRMSG, This); A the } + - Virtual voidNotify (stringstrmes) $ { $cout<<"colleague 1 Gets the message:"<< strmes<<Endl; - } - }; the - classCONCRETECOLLEAGUEB: PubliccolleagueWuyi { the Public: -Concretecolleagueb (mediator*Pstmediator): Colleague (Pstmediator) Wu { - About } $ - Virtual voidSend (stringSTRMSG) - { -M_pstmediator->send (STRMSG, This); A + } the - Virtual voidNotify (stringstrmes) $ { thecout<<"colleague 2 Gets the message:"<< strmes<<Endl; the } the }; the - classConcretemediator: PublicMediator in { the Public: the voidSetcolleaguea (concretecolleaguea*pstcolleague) About { theM_pstcolleaguea =Pstcolleague; the } the + voidSetcolleagueb (concretecolleagueb*pstcolleague) - { theM_pstcolleagueb =Pstcolleague;Bayi } the the Virtual voidSend (stringStrmes, colleague*pstcolleague) - { - if(Pstcolleague = =M_pstcolleaguea) the { theM_pstcolleagueb->Notify (strmes); the } the Else - { theM_pstcolleaguea->Notify (strmes); the } the }94 the Private: theconcretecolleaguea*M_pstcolleaguea; theconcretecolleagueb*M_pstcolleagueb;98 }; About - intMainintargcChar*argv[])101 {102concretemediator* Pstmediatora =Newconcretemediator ();103 104concretecolleaguea* Pstcolleaguea =NewConcretecolleaguea (Pstmediatora); theconcretecolleagueb* Pstcolleagueb =NewConcretecolleagueb (Pstmediatora);106 107Pstmediatora->Setcolleaguea (Pstcolleaguea);108Pstmediatora->Setcolleagueb (PSTCOLLEAGUEB);109 the 111Pstcolleaguea->send ("How is it ?"); thePstcolleagueb->send ("I am Fine, Thank you!");113 the return 0; the } the ////////////////////////////////////////////117[Email protected] ~/learn_code/design_pattern/21_mediator]$.Mediator118Colleague 2 Get the message: How is it?119Colleague 1 Get message: I am Fine, Thank you!
Design pattern-Mediator Mode/Mediator mode (c + + implementation)