中介者模式.cpp
# include <iostream>
# include <map>
# include <memory>
using namespace std;
namespace ns1
{ class CtlParent { protected : string m_caption; public : virtual ~ CtlParent ( ) { } CtlParent ( const string & caption) : m_caption ( caption) { } public : virtual void Changed ( map< string, shared_ptr< CtlParent>> & tmpuictllist) = 0 ; virtual void Enable ( bool sign) const = 0 ; } ; class Button : public CtlParent { public : Button ( const string & caption) : CtlParent ( caption) { } public : void Enable ( bool sign) const override { if ( sign) cout << "button \"" << m_caption << "\" enable" << endl; else cout << "button \"" << m_caption << "\" disable" << endl; } void Changed ( map< string, shared_ptr< CtlParent>> & tmpuictllist) override ; } ; class RadioBtn : public CtlParent { public : RadioBtn ( const string & caption) : CtlParent ( caption) { } public : void Enable ( bool sign) const override { } void Selected ( bool sign) const { if ( sign) cout << "radio button \"" << m_caption << "\" selected" << endl; else cout << "radio button \"" << m_caption << "\" not selected" << endl; } void Changed ( map< string, shared_ptr< CtlParent>> & tmpuictllist) override ; } ; class EditCtl : public CtlParent { string m_content = "" ; public : EditCtl ( const string & caption) : CtlParent ( caption) { } public : void Enable ( bool sign) const override { if ( sign) cout << "edit box \"" << m_caption << "\" enable" << endl; else cout << "edit box \"" << m_caption << "\" disable" << endl; } bool isContentEmpty ( ) const { return m_content. empty ( ) ; } void Changed ( map< string, shared_ptr< CtlParent>> & tmpuictllist) override ; } ; void Button :: Changed ( map< string, shared_ptr< CtlParent>> & tmpuictllist) { if ( m_caption == "sign in" ) cout << "Start the game login verification, and decide whether to enter the game or give a prompt according to the verification result!" << endl; else if ( m_caption == "exit" ) cout << "Game exit, goodbye!" << endl; } void RadioBtn :: Changed ( map< string, shared_ptr< CtlParent>> & tmpuictllist) { if ( m_caption == "visitor login" ) { static_pointer_cast < RadioBtn> ( tmpuictllist[ "visitor login" ] ) -> Selected ( true ) ; static_pointer_cast < RadioBtn> ( tmpuictllist[ "account login" ] ) -> Selected ( false ) ; tmpuictllist[ "account" ] -> Enable ( false ) ; tmpuictllist[ "password" ] -> Enable ( false ) ; tmpuictllist[ "sign in" ] -> Enable ( true ) ; } else if ( m_caption == "account login" ) { static_pointer_cast < RadioBtn> ( tmpuictllist[ "visitor login" ] ) -> Selected ( false ) ; static_pointer_cast < RadioBtn> ( tmpuictllist[ "account login" ] ) -> Selected ( true ) ; tmpuictllist[ "account" ] -> Enable ( true ) ; tmpuictllist[ "password" ] -> Enable ( true ) ; if ( static_pointer_cast < EditCtl> ( tmpuictllist[ "account" ] ) -> isContentEmpty ( ) || static_pointer_cast < EditCtl> ( tmpuictllist[ "password" ] ) -> isContentEmpty ( ) ) tmpuictllist[ "sign in" ] -> Enable ( false ) ; else tmpuictllist[ "sign in" ] -> Enable ( true ) ; } } void EditCtl :: Changed ( map< string, shared_ptr< CtlParent>> & tmpuictllist) { if ( static_pointer_cast < EditCtl> ( tmpuictllist[ "account" ] ) -> isContentEmpty ( ) || static_pointer_cast < EditCtl> ( tmpuictllist[ "password" ] ) -> isContentEmpty ( ) ) tmpuictllist[ "sign in" ] -> Enable ( false ) ; else tmpuictllist[ "sign in" ] -> Enable ( true ) ; }
} namespace ns2
{ class CtlParent ; class Mediator { public : virtual ~ Mediator ( ) { } public : virtual void createCtrl ( ) = 0 ; virtual void ctlChanged ( CtlParent * const ) = 0 ; } ; class CtlParent { protected : Mediator * m_pmediator; string m_caption; public : virtual ~ CtlParent ( ) { } CtlParent ( Mediator * const ptmpm, const string & caption) : m_pmediator ( ptmpm) , m_caption ( caption) { } public : virtual void Changed ( ) { m_pmediator-> ctlChanged ( this ) ; } virtual void Enable ( bool sign) = 0 ; } ; class Button : public CtlParent { public : Button ( Mediator * const ptmpm, const string & caption) : CtlParent ( ptmpm, caption) { } void Enable ( bool sign) override { if ( sign == true ) cout << "button \"" << m_caption << "\" enable" << endl; else cout << "button \"" << m_caption << "\" disable" << endl; } } ; class RadioBtn : public CtlParent { public : RadioBtn ( Mediator * const ptmpm, const string & caption) : CtlParent ( ptmpm, caption) { } void Enable ( bool sign) override { } void Selected ( bool sign) { if ( sign == true ) cout << "radio button \"" << m_caption << "\" selected" << endl; else cout << "radio button \"" << m_caption << "\" not selected" << endl; } } ; class EditCtl : public CtlParent { string m_content = "" ; public : EditCtl ( Mediator * const ptmpm, const string & caption) : CtlParent ( ptmpm, caption) { } void Enable ( bool sign) override { if ( sign == true ) cout << "edit box \"" << m_caption << "\" enable" << endl; else cout << "edit box \"" << m_caption << "\" disable" << endl; } bool isContentEmpty ( ) const { return m_content. empty ( ) ; } } ; class concrMediator : public Mediator { public : shared_ptr< Button> mp_login; shared_ptr< Button> mp_logout; shared_ptr< RadioBtn> mp_rbtn1; shared_ptr< RadioBtn> mp_rbtn2; shared_ptr< EditCtl> mp_edtctl1; shared_ptr< EditCtl> mp_edtctl2; public : void createCtrl ( ) override { mp_login = make_shared < Button> ( this , "sign in" ) ; mp_logout = make_shared < Button> ( this , "exit" ) ; mp_rbtn1 = make_shared < RadioBtn> ( this , "visitor login" ) ; mp_rbtn2 = make_shared < RadioBtn> ( this , "account login" ) ; mp_edtctl1 = make_shared < EditCtl> ( this , "account editing box" ) ; mp_edtctl2 = make_shared < EditCtl> ( this , "password editing box" ) ; mp_rbtn1-> Selected ( true ) ; mp_rbtn2-> Selected ( false ) ; mp_edtctl1-> Enable ( false ) ; mp_edtctl2-> Enable ( false ) ; mp_login-> Enable ( true ) ; mp_logout-> Enable ( true ) ; } virtual void ctlChanged ( CtlParent * const p_ctrl) { if ( p_ctrl == mp_login. get ( ) ) cout << "Start the game login verification, and decide whether to enter the game or give a prompt according to the verification result!" << endl; else if ( p_ctrl == mp_logout. get ( ) ) cout << "Game exit, goodbye!" << endl; if ( p_ctrl == mp_rbtn1. get ( ) ) { mp_rbtn1-> Selected ( true ) ; mp_rbtn2-> Selected ( false ) ; mp_edtctl1-> Enable ( false ) ; mp_edtctl2-> Enable ( false ) ; mp_login-> Enable ( true ) ; } else if ( p_ctrl == mp_rbtn2. get ( ) ) { mp_rbtn1-> Selected ( false ) ; mp_rbtn2-> Selected ( true ) ; mp_edtctl1-> Enable ( true ) ; mp_edtctl2-> Enable ( true ) ; if ( mp_edtctl1-> isContentEmpty ( ) || mp_edtctl2-> isContentEmpty ( ) ) mp_login-> Enable ( false ) ; else mp_login-> Enable ( true ) ; } if ( p_ctrl == mp_edtctl1. get ( ) || p_ctrl == mp_edtctl2. get ( ) ) if ( mp_edtctl1-> isContentEmpty ( ) || mp_edtctl2-> isContentEmpty ( ) ) mp_login-> Enable ( false ) ; else mp_login-> Enable ( true ) ; } } ;
} int main ( )
{
# if 0 using namespace ns1; map< string, shared_ptr< CtlParent>> uictllist; uictllist[ "sign in" ] = make_shared < Button> ( "sign in" ) ; uictllist[ "exit" ] = make_shared < Button> ( "exit" ) ; uictllist[ "visitor login" ] = make_shared < RadioBtn> ( "visitor login" ) ; uictllist[ "account login" ] = make_shared < RadioBtn> ( "account login" ) ; uictllist[ "account" ] = make_shared < EditCtl> ( "account" ) ; uictllist[ "password" ] = make_shared < EditCtl> ( "password" ) ; static_pointer_cast < RadioBtn> ( uictllist[ "visitor login" ] ) -> Selected ( true ) ; static_pointer_cast < RadioBtn> ( uictllist[ "account login" ] ) -> Selected ( false ) ; uictllist[ "account" ] -> Enable ( false ) ; uictllist[ "password" ] -> Enable ( false ) ; uictllist[ "sign in" ] -> Enable ( true ) ; uictllist[ "exit" ] -> Enable ( true ) ; cout << "--------------------------" << endl; uictllist[ "account login" ] -> Changed ( uictllist) ;
# endif # if 1 using namespace ns2; concrMediator mymedia; mymedia. createCtrl ( ) ; cout << "-------------when \"account login\" radio button pressed:-------------" << endl; mymedia. mp_rbtn2-> Changed ( ) ;
# endif cout << "Over!\n" ; return 0 ;
}