Design Patterns Reference: Mediator

The Mediator Design Pattern is used to develop an object that communicates or mediates changes to a collection of similar objects without them interacting with each other directly.
class CD
{
public $band = ”;
public $title = ”;
protected $_mediator;

public function __construct($mediator = null)
{
$this->_mediator = $mediator;
}

public function save()
{
// stub – writes data back to database – use this to verify
var_dump($this);
}

public function changeBandName($newName)
{
$this->band = $newName;
$this->save();
}
}

class MP3Archive
{
public $band = ”;
public $title = ”;
protected $_mediator;

public function __construct($mediator = null)
{
$this->_mediator = $mediator;
}

public function save()
{
// stub – writes data back to database – use this to verify

var_dump($this);
}

public function changeBandName($newName)
{
if (!is_null($this->_mediator)){
$this->_mediator->change($this, array(‘band’=>$newName));
}
$this->band = $newName;
$this->save();
}
}

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 (!($changedObject instanceof $container)){
$object = new $container;
$object->title = $title;
$object->band = $band;

foreach ($newValue as $key=>$value){
$object->key = $val;
}

$object->save();
}
}
}
}

// To use the new Mediator object

$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’);

source: http://www.wiley.com/WileyCDA/WileyTitle/productCd-0470496703,descCd-DOWNLOAD.html

Leave a Reply

You must be logged in to post a comment.