PHP-设计模式之-中介者模式

<?php
//中介者模式 --
//抽象中介者
abstract class UnitedNationa{
punlic abstract function Declared($message,country $colleague)
}

class UnitedCommit extends UnitedNationa{
punlic $countryUsa;
punlic $countryChina;
punlic function Declared($message,Country $colleague){
if ($colleague == $this->countryChina) {
$this->countryUsa->GetMessage()
} else {
$this->countryChina->GetMessage()
}
}
}

//抽象国家
abstract class Country{
protected $mediator;
public function construct(UnitedNations $_mediator){
$this->mediator = $_mediator
}
}

//具体国家类
class USA extends Country {
function __construct(UnitedNations $mediator){
parent::__construct($mediator);
}
//声明
public function Declared($mediator){
$this->mediator->Declared($message,$this);
}
//获得消息
public function GetMessage($message){
echo "获得消息:$message<br/>";
}

}

class china extends Country{
public function __construct(UnitedNations,$mediator){
parent::__construct($mesiator);
}
//声明
public function Declared($message){
$this->declared($message,$this);
}
//获取消息
public function GetMessage($message){
echo "得到消息:$message<br/>";
}
}

 

$zj = new UnitedCommit();
$c1 = new USA($zj);
$c2 = new China($zj);

$zj->countryChina = $c2
$zj->countryUSA = $c1

$c1->Declared('12');
$c2->Declared('23');

posted @ 2019-03-30 13:30  千载白云  阅读(225)  评论(0编辑  收藏  举报