PHP design mode Series-Broker mode _php Tutorial

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:
We have a CD class and a MP3 class, and the structure of the two classes is similar.
We need to update the MP3 class synchronously when the CD class is updated.
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.
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.
This design pattern was previously useful in our Phpwind forum.
Code:
[PHP]
Class CD {
Public $band = ';
public $title = ';
protected $_mediator;

Public function __construct (musiccontainermediator $mediator = NULL) {
$this->_mediator = $mediator;
}

Public Function Save () {
Concrete implementation to be determined
Var_dump ($this);
}

Public Function Changebandname ($bandname) {
if (! Is_null ($this->_mediator)) {
$this->_mediator->change ($this, Array ("band" and "= $bandname)");
}
$this->band = $bandname;
$this->save ();
}
}
Mp3archive class
Class Mp3archive {
protected $_mediator;

Public function __construct (musiccontainermediator $mediator = NULL) {
$this->_mediator = $mediator;
}

Public Function Save () {
Concrete implementation to be determined
Var_dump ($this);
}

Public Function Changebandname ($bandname) {
if (! Is_null ($this->_mediator)) {
$this->_mediator->change ($this, Array ("band" and "= $bandname)");
}
$this->band = $bandname;
$this->save ();
}

}

Intermediary class
Class Musiccontainermediator {
Protected $_containers = Array ();

Public Function __construct () {
$this->_containers[] = "CD";
$this->_containers[] = "mp3archive";
}

Public function Change ($originalObject, $newValue) {
$title = $originalObject->title;
$band = $originalObject->band;
foreach ($this->_containers as $container) {
if (! ($originalObject instanceof $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 = new Musiccontainermediator ();
$CD = new CD ($mediator);
$CD->title = $titleFromDB;
$CD->band = $bandFromDB;
$cd->changebandname ("Maybe Once more");
Author: initphp

http://www.bkjia.com/PHPjc/478130.html www.bkjia.com true http://www.bkjia.com/PHPjc/478130.html techarticle The Mediator pattern 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. ...

  • Related Article

    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.