Copy codeThe Code is as follows:
<? Php
/**
* Intermediary Mode
*
* An intermediary object is used to encapsulate a series of object interactions, so that each object does not need to be explicitly referenced to make the coupling loose and the interaction between them can be changed independently.
*/
Abstract class Mediator
{
Abstract public function send ($ message, $ colleague );
}
Abstract class Colleague
{
Private $ _ mediator = null;
Public function Colleague ($ mediator)
{
$ This-> _ mediator = $ mediator;
}
Public function send ($ message)
{
$ This-> _ mediator-> send ($ message, $ this );
}
Abstract public function notify ($ message );
}
Class ConcreteMediator extends Mediator
{
Private $ _ colleague1 = null;
Private $ _ colleague2 = null;
Public function send ($ message, $ colleague)
{
If ($ colleague ==$ this-> _ colleague1)
{
$ This-> _ colleague1-> Policy ($ message );
} Else {
$ This-> _ colleague2-> Policy ($ message );
}
}
Public function set ($ colleague1, $ colleague2)
{
$ This-> _ colleague1 = $ colleague1;
$ This-> _ colleague2 = $ colleague2;
}
}
Class Colleague1 extends Colleague
{
Public function notify ($ message)
{
Echo "Colleague1 Message is:". $ message. "<br/> ";
}
}
Class Colleague2 extends Colleague
{
Public function notify ($ message)
{
Echo "Colleague2 Message is:". $ message. "<br/> ";
}
}
//
$ ObjMediator = new ConcreteMediator ();
$ ObjC1 = new Colleague1 ($ objMediator );
$ ObjC2 = new Colleague2 ($ objMediator );
$ ObjMediator-> set ($ objC1, $ objC2 );
$ ObjC1-> send ("to c2 from c1 ");
$ ObjC2-> send ("to c1 from c2 ");