Mediator (C ++ implementation)

Source: Internet
Author: User

Recommended articles: http://blog.csdn.net/roynee/archive/2009/10/05/4634197.aspx

 

The following is a simple implementation: (reprinted)

Class Mediator;

Class colleage
{
Public:
Virtual ~ Colleage (){};
Virtual void action () = 0;
Virtual void setstate (const string & SD) = 0;
Virtual string getstate () = 0;
Protected:
Colleage (){};
Colleage (mediator * MDT) {This-> m_mdt = MDT ;};
Mediator * m_mdt;
};

Class concretecolleagea: Public colleage
{
Public:
Concretecolleagea (){};
Concretecolleagea (mediator * MDT): colleage (MDT ){}
~ Concretecolleagea (){};
Void setstate (const string & SD) {m_sd = SD ;}
String getstate () {return m_sd ;}
Void action ();
PRIVATE:
String m_sd;
}; Class concretecolleageb: Public colleage
{
Public:
Concretecolleageb (){};
Concretecolleageb (mediator * MDT): colleage (MDT ){}
~ Concretecolleageb (){};
Void setstate (const string & SD) {m_sd = SD ;};
String getstate () {return m_sd ;}
Void action ();
PRIVATE:
String m_sd;
}; Class Mediator
{
Public:
Virtual ~ Mediator (){};
Virtual void doactionfromatob () = 0;
Virtual void doactionfrombtoa () = 0;
Protected:
Mediator (){};
}; Class concretemediator: public mediator
{
Public:
Concretemediator (){};
Concretemediator (colleage * linoleic, colleage * CLB) {m_linoleic = linoleic; m_clb = CLB ;};
~ Concretemediator (){};
Void setcona (colleage * linoleic) {m_linoleic = linoleic ;}
Void setconb (colleage * CLB) {m_clb = CLB ;}
Colleage * getcona () {return m_linoleic ;}
Colleage * getconb () {return m_clb ;}
Void doactionfromatob () {m_clb-> setstate (m_linoleic-> getstate ());}
Void doactionfrombtoa () {m_linoleic-> setstate (m_clb-> getstate ());}
Void introcolleage (colleage * linoleic, colleage * CLB)
{
M_linoleic = linoleic; m_clb = CLB;
}
PRIVATE:
Colleage * m_linoleic;
Colleage * m_clb;
}; Void concretecolleagea: Action ()
{
M_mdt-> doactionfromatob ();
Cout <"Conb" <this-> getstate () <Endl;
} Void concretecolleageb: Action ()
{
M_mdt-> doactionfrombtoa ();
Cout <"cona" <this-> getstate () <Endl;
} Void main ()
{
Concretemediator * m = new concretemediator ();
Concretecolleagea * C1 = new concretecolleagea (m );
Concretecolleageb * C2 = new concretecolleageb (m );
M-> introcolleage (C1, C2); C1-> setstate ("jackill ");
C1-> action ();
C2-> setstate ("rukawa ");
C2-> action ();
Cout <Endl; C1-> setstate ("new ");
C1-> action ();
C2-> action ();
Cout <Endl;
}

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.