php调停者模式(mediator pattern)
发布时间:2020-12-13 16:07:55 所属栏目:PHP教程 来源:网络整理
导读:差不多了,睡一觉,下次再弄。 ?php/*The more classes we have in our software,the more complex their communicationbecomes. The mediator pattern addresses this complexity by encapsulating it intoa mediator object. Objects no longer communicat
差不多了,睡一觉,下次再弄。 <?php /* The more classes we have in our software,the more complex their communication becomes. The mediator pattern addresses this complexity by encapsulating it into a mediator object. Objects no longer communicate directly,but rather through a mediator object,therefore lowering the overall coupling. */ interface MediatorInterface { public function fight(); public function talk(); public function registerA(ColleagueA $a); public function registerB(ColleagueB $b); } class ConcreteMediator implements MediatorInterface { protected $talk; protected $fight; public function registerA(ColleagueA $a) { $this->talk = $a; } public function registerB(ColleagueB $b) { $this->fight = $b; } public function fight() { echo ‘fighting...<br/>‘; } public function talk() { echo ‘talking...<br/>‘; } } abstract class Colleague { protected $mediator; public abstract function doSomething(); } class ColleagueA extends Colleague { public function __construct(MediatorInterface $mediator) { $this->mediator = $mediator; $this->mediator->registerA($this); } public function doSomething() { $this->mediator->talk(); } } class ColleagueB extends Colleague { public function __construct(MediatorInterface $mediator) { $this->mediator = $mediator; $this->mediator->registerB($this); } public function doSomething() { $this->mediator->fight(); } } $mediator = new ConcreteMediator(); $talkColleague = new ColleagueA($mediator); $fightColleague = new ColleagueB($mediator); $talkColleague->doSomething(); $fightColleague->doSomething(); ?> 输出: talking... fighting... (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |