中介者模式
C++
# include <iostream>
# include <string> using namespace std; # define SAFE_DELETE ( p) \ if ( p) \ { \ delete p; \ p = NULL \ } class Mediator ;
class Colleague
{
public : Colleague ( Mediator * pMediator) : m_pMediator ( pMediator) { } ; virtual void Send ( char * message) = 0 ; protected : Mediator * m_pMediator;
} ;
class ConcreteColleague1 : public Colleague
{
public : ConcreteColleague1 ( Mediator * pMediator) : Colleague ( pMediator) { } ; virtual void Send ( char * message) ; void Notify ( char * message) { cout << "同事1得到消息:" << message << endl; }
} ;
class ConcreteColleague2 : public Colleague
{
public : ConcreteColleague2 ( Mediator * pMediator) : Colleague ( pMediator) { } ; virtual void Send ( char * message) ; void Notify ( char * message) { cout << "同事2得到消息:" << message << endl; }
} ;
class Mediator
{
public : virtual void Send ( char * message, Colleague * pColleague) = 0 ;
} ; void ConcreteColleague1 :: Send ( char * message)
{ m_pMediator-> Send ( message, this ) ;
} void ConcreteColleague2 :: Send ( char * message)
{ m_pMediator-> Send ( message, this ) ;
}
class ConcreteMediator : public Mediator
{
public : void Send ( char * message, Colleague * pColleague) { ConcreteColleague1 * pConcreteColleague1 = dynamic_cast < ConcreteColleague1 * > ( pColleague) ; if ( pConcreteColleague1) { cout << "消息来自同事1" << endl; if ( m_pColleague2) { m_pColleague2-> Notify ( message) ; } } else { cout << "消息来自同事2" << endl; if ( m_pColleague1) { m_pColleague1-> Notify ( message) ; } } } void setColleague1 ( Colleague * pColleague) { m_pColleague1 = dynamic_cast < ConcreteColleague1 * > ( pColleague) ; } void setColleague2 ( Colleague * pColleague) { m_pColleague2 = dynamic_cast < ConcreteColleague2 * > ( pColleague) ; } private : ConcreteColleague1 * m_pColleague1; ConcreteColleague2 * m_pColleague2;
} ; int main ( int argc, char * * argv)
{ Mediator * pMediator = new ConcreteMediator ( ) ; Colleague * pColleague1 = new ConcreteColleague1 ( pMediator) ; Colleague * pColleague2 = new ConcreteColleague2 ( pMediator) ; ConcreteMediator * pConcreteMediator = dynamic_cast < ConcreteMediator * > ( pMediator) ; pConcreteMediator-> setColleague1 ( pColleague1) ; pConcreteMediator-> setColleague2 ( pColleague2) ; char message1[ 128 ] = "你吃饭了吗?" ; pColleague1-> Send ( message1) ; char message2[ 128 ] = "还没呢" ; pColleague2-> Send ( message2) ; delete pColleague1; delete pColleague2; delete pMediator; return 0 ;
}
C
# include <stdio.h>
# include <stdlib.h>
# include <string.h> typedef struct Mediator Mediator;
typedef struct Colleague Colleague;
typedef struct Colleague
{ void ( * send) ( Colleague * self, char * message) ; void ( * notify) ( Colleague * self, char * message) ; Mediator * mediator;
} Colleague;
typedef struct Mediator
{ void ( * send) ( Mediator * self, char * message, Colleague * pColleague) ; Colleague * colleague1; Colleague * colleague2;
} Mediator;
Colleague * create_colleague ( void ( * send) ( Colleague * self, char * message) , void ( * notify) ( Colleague * self, char * message) )
{ Colleague * colleague = malloc ( sizeof ( Colleague) ) ; colleague-> send = send; colleague-> notify = notify; colleague-> mediator = NULL ; return colleague;
}
Mediator * create_mediator ( )
{ Mediator * mediator = malloc ( sizeof ( Mediator) ) ; mediator-> send = NULL ; mediator-> colleague1 = NULL ; mediator-> colleague2 = NULL ; return mediator;
}
void concrete_colleague1_send ( Colleague * self, char * message)
{ self-> mediator-> send ( self-> mediator, message, self) ;
}
void concrete_colleague1_notify ( Colleague * self, char * message)
{ printf ( "同事1得到消息:%s\n" , message) ;
}
void concrete_colleague2_send ( Colleague * self, char * message)
{ self-> mediator-> send ( self-> mediator, message, self) ;
}
void concrete_colleague2_notify ( Colleague * self, char * message)
{ printf ( "同事2得到消息:%s\n" , message) ;
}
void concrete_mediator_send ( Mediator * self, char * message, Colleague * pColleague)
{ if ( self-> colleague1 == pColleague) { printf ( "消息来自同事1\n" ) ; if ( self-> colleague2 != NULL ) { self-> colleague2-> notify ( self-> colleague2, message) ; } } else { printf ( "消息来自同事2\n" ) ; if ( self-> colleague1 != NULL ) { self-> colleague1-> notify ( self-> colleague1, message) ; } }
} int main ( )
{ Mediator * mediator = create_mediator ( ) ; mediator-> send = concrete_mediator_send; Colleague * colleague1 = create_colleague ( concrete_colleague1_send, concrete_colleague1_notify) ; Colleague * colleague2 = create_colleague ( concrete_colleague2_send, concrete_colleague2_notify) ; colleague1-> mediator = mediator; colleague2-> mediator = mediator; mediator-> colleague1 = colleague1; mediator-> colleague2 = colleague2; char message1[ ] = "你吃饭了吗?" ; colleague1-> send ( colleague1, message1) ; char message2[ ] = "还没呢" ; colleague2-> send ( colleague2, message2) ; free ( colleague1) ; free ( colleague2) ; free ( mediator) ; return 0 ;
}