引自原书
1.中介者模式很容易在系统中应用,也很容易在系统中误用。当系统出现了‘多对多’交互复杂的对象群时,不要急于使用中介者模式,而要先反思你的系统在设计上是不是合理。
2.Mediator的出现减少了各个Colleague的耦合,使得可以读一地改变和服用各个Colleague类和Mediator。
3.由于把对象如何协作进行了抽象,将中介作为一个独立的概念并将其封装在一个对象中,这样关注的对象就从对象各自本身的行为转移到他们之间的交互上来,也就是从一个更宏观的角度去看待系统。
4.由于ConcreteMediator控制了集中化,于是就把交互复杂性变成了中介者的复杂性这就使得中介者会比任何一个ConcreteColleague都复杂
5.中介者模式一般应用于一组对象以及定义良好但是复杂的方式进行通信的场合,以及向定制一个分布在多个类中的行为,而不想生成太多子类的场合。
//抽象中介者类 abstract class Mediator { public abstract function send($message, Colleague $colleague); } //抽象同事类 abstract class Colleague { protected $mediator; function __construct(Mediator $mediator) { $this->mediator = $mediator; } } //具体中介者类 class ConcreteMediator extends Mediator{ private $colleague1; private $colleague2; public function __get($key) { return isset($this->$key) ? $this->$key : null; } public function __set($key, $value) { $this->$key = $value; } public function send($message, Colleague $colleague) { //重写发送信息的方法,根据对象做出选择判断,通知对象 switch($colleague) { case $this->colleague1: $this->colleague2->notify($message); break; case $this->colleague2: $this->colleague1->notify($message); break; } } } //各类同事对象 class ConcreteColleague1 extends Colleague { function __construct(Mediator $mediator) { parent::__construct($mediator); } public function send($message) { $this->mediator->send($message, $this); } public function notify($message) { echo '同事1得到信息:'.$message; } } class ConcreteColleague2 extends Colleague { function __construct(Mediator $mediator) { parent::__construct($mediator); } public function send($message) { $this->mediator->send($message, $this); } public function notify($message) { echo '同事2得到信息:'.$message; } } $m = new ConcreteMediator(); $c1 = new ConcreteColleague1($m); $c2 = new ConcreteColleague2($m); $m->colleague1 = $c1; $m->colleague2 = $c2; $c1->send('吃过饭了码?<br/>', $c1); $c2->send('没有呢,你打算请客?<br/>', $c2);
//联合国机构 abstract class UnitedNations { //声明 public abstract function myDeclare($message, Country $colleague); } //国家 abstract class Country { protected $mediator; function __construct(UnitedNations $mediator) { $this->mediator = $mediator; } } //美国 class USA extends Country { function __construct(UnitedNations $mediator) { parent::__construct($mediator); } //声明 public function myDeclare($message) { $this->mediator->myDeclare($message, $this); } //获得消息 public function getMessage($message) { echo '美国获得对方信息:'.$message; } } //伊拉克 class Iraq extends Country { function __construct(UnitedNations $mediator) { parent::__construct($mediator); } //声明 public function myDeclare($message) { $this->mediator->myDeclare($message, $this); } //获得消息 public function getMessage($message) { echo '伊拉克获得对方信息:'.$message; } } //联合国安全理事会 class UnitedNationsSecurityCouncil extends UnitedNations { private $colleague1; private $colleague2; public function __get($key) { return isset($this->$key) ? $this->$key : null; } public function __set($key, $value) { $this->$key = $value; } //声明 public function myDeclare($message, Country $colleague) { switch($colleague) { case $this->colleague1: $this->colleague2->getMessage($message); break; case $this->colleague2: $this->colleague1->getMessage($message); break; } } } $UNSC = new UnitedNationsSecurityCouncil(); $c1 = new USA($UNSC); $c2 = new Iraq($UNSC); $UNSC->colleague1 = $c1; $UNSC->colleague2 = $c2; $c1->myDeclare('不准研制核武器,否则要发动战争!<br/>'); $c2->myDeclare('我们没有核武器,也不怕侵略。');