Home  >  Article  >  Backend Development  >  com.cooliris.media php design pattern Mediator mediator pattern

com.cooliris.media php design pattern Mediator mediator pattern

WBOY
WBOYOriginal
2016-07-29 08:45:44959browse

复制代码 代码如下:


/**
* Mediator Pattern
*
* Use a mediator object to encapsulate a series of object interactions so that each object does not need to explicitly reference each other, thus loosening the coupling, 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->notify($message);
} else {
$this->_colleague2->notify($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."
";
}
}
class Colleague2 extends Colleague
{
public function notify($message)
{
echo "Colleague2 Message is :".$message."
";
}
}
//
$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");

以上就介绍了com.cooliris.media php设计模式 Mediator 中介者模式,包括了com.cooliris.media方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn