PHP design mode Mediator (mediator mode)

Source: Internet
Author: User
Copy CodeThe code is as follows:
<?php
/**
* Intermediary mode
*
* Use a Mediation object to encapsulate a series of object interactions so that the objects do not need to be explicitly referenced to each other so that they are loosely coupled and can independently change their interactions
*/
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->notify ($message);
} else {
$this->_colleague2->notify ($message);
}
}
Public function set ($colleague 1, $colleague 2)
{
$this->_colleague1 = $colleague 1;
$this->_colleague2 = $colleague 2;
}
}
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 ();
$objC 1 = new Colleague1 ($objMediator);
$objC 2 = new Colleague2 ($objMediator);
$objMediator->set ($objC 1, $objC 2);
$objC 1->send ("to C2 from C1");
$objC 2->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.