Design pattern-Mediator Mode/Mediator mode (c + + implementation)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.