The mediator pattern is used to develop an object that can transfer or mediate the modification of a collection of these objects in situations where similar objects are not directly connected to each other. When dealing with non-coupled objects that have similar properties and need to remain synchronized, the best practice is to broker mode. PHP is not a particularly common design pattern.
<?phpabstract class Mediator {//Mediator role abstract public function send ($message, $colleague);} abstract class Colleag UE {//abstract object Private $_mediator = null; Public function __construct ($mediator) {$this->_mediator = $mediator; The Public function send ($message) {$this->_mediator->send ($message, $this); } Abstract Public Function notify ($message); } class Concretemediator extends Mediator {//specific mediator role private $_colleague1 = null; Private $_colleague2 = null; Public function Send ($message, $colleague) {//echo $colleague->notify ($message); if ($colleague = = $this->_colleague1) {$this->_colleague1->notify ($message); } else {$this->_colleague2->notify ($message); }} Public function set ($colleague 1, $colleague 2) {$this->_colleague1 = $colleague 1; $this->_colleague2 = $colleague 2; }} class Colleague1 extends colleague {//specificObject Role Public Function notify ($message) {echo ' colleague1: '. $message. ' <br> "; }} class Colleague2 extends colleague {//specific object role Public function notify ($message) {echo ' colleague2: '. $messa GE. " <br> "; }}//Client$objmediator = new Concretemediator (); $objC 1 = new Colleague1 ($objMediator); $objC 2 = new Colleague2 ($objMediator); $objMediator->set ($objC 1, $objC 2); $objC 1->send ("to C2 from C1"); Output: Colleague1:to C2 from C1$objc2->send ("to C1 from C2"); Output: Colleague2:to C1 from C2
PHP design mode-broker mode