PHP design mode series-Broker mode

Source: Internet
Author: User

    • Broker mode

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.

    • Design Scenario:
      1. We have a CD class and a MP3 class, and the structure of the two classes is similar.
      2. We need to update the MP3 class synchronously when the CD class is updated.
      3. The traditional approach is to instantiate the MP3 class in the CD class and then update it, but if you do, the code will be difficult to maintain, and if you add the same MP4 class, you won't be able to handle it.
      4. The mediator pattern is a good deal of this situation, through the Mediator class, the CD class, as long as the call to the intermediary class, you can update the data synchronously.
      5. This design pattern was previously useful in our Phpwind forum.
    • Code:
<?PHPclassCD { Public $band= ' ';  Public $title= ' '; protected $_mediator;  Public function__construct (Musiccontainermediator$mediator=NULL) {                             $this->_mediator =$mediator; }                     Public functionSave () {//Concrete implementation to be determined            Var_dump($this); }                     Public functionChangebandname ($bandname) {                             if( !Is_null($this-_mediator)) {                                     $this->_mediator->change ($this,Array("Band" =$bandname)); }                             $this->band =$bandname; $this-Save (); }               }      //Mp3archive class    classmp3archive {protected $_mediator;  Public function__construct (Musiccontainermediator$mediator=NULL) {                             $this->_mediator =$mediator; }                                           Public functionSave () {//Concrete implementation to be determined            Var_dump($this); }                                           Public functionChangebandname ($bandname) {                             if( !Is_null($this-_mediator)) {                                     $this->_mediator->change ($this,Array("Band" =$bandname)); }                             $this->band =$bandname; $this-Save (); }                       }                //Intermediary class    classMusiccontainermediator {protected $_containers=Array();  Public function__construct () {$this->_containers[] = "CD"; $this->_containers[] = "Mp3archive"; }                                           Public functionChange$originalObject,$newValue) {                             $title=$originalObject-title; $band=$originalObject-Band; foreach($this->_containers as $container) {                                     if( ! ($originalObjectinstanceof$container)) {                                            $object=New $container; $object->title =$title; $object->band =$band; foreach($newValue  as $key=$val) {                                          $object-$key=$val; }                                                                    $object-Save (); }                             }                     }             }               //Test Example    $titleFromDB= "Waste of a Rib"; $bandFromDB= "Never Again"; $mediator=NewMusiccontainermediator (); $CD=NewCD ($mediator); $CD->title =$titleFromDB; $CD->band =$bandFromDB; $CD->changebandname ("Maybe Once more");

From: http://blog.csdn.net/initphp/article/details/7695805

PHP design mode series-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.