Php design mode Mediator (intermediary Mode)

Source: Internet
Author: User

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 ");

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.