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. ...