php调停者模式(mediator pattern)

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php调停者模式(mediator pattern)脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

差不多了,睡一觉,下次再弄。

<?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...

脚本宝典总结

以上是脚本宝典为你收集整理的php调停者模式(mediator pattern)全部内容,希望文章能够帮你解决php调停者模式(mediator pattern)所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。