PHP design mode-broker mode

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.