added Decorator pattern

This commit is contained in:
Dominik Liebler
2011-08-26 19:29:16 +02:00
parent 1e15c7b6a4
commit 11d64bf50d

84
Decorator/Decorator.php Normal file
View File

@@ -0,0 +1,84 @@
<?php
namespace DesignPatterns;
/**
* Decorator pattern
*
* Purpose:
* to dynamically add new functionality to class instances
*
* Examples:
* - Zend Framework: decorators for Zend_Form_Element instances
* - Web Service Layer: Decorators JSON and XML for a REST service (in this case, only one of these should be allowed of
* course)
*
*/
class Webservice
{
protected $_data;
/**
* an array to holds all added decorators, often there would be defaults set in this
* array, e.g. the service could be defaulted to use JSON and only for special APIs
* use XML
*
* @var array
*/
protected $_decorators = array();
public function __construct($data)
{
$this->_data = $data;
}
/**
*
*
* @param WebserviceDecorator $decorator
* @return void
*/
public function addDecorator(WebserviceDecorator $decorator)
{
$this->_decorators[] = $decorator;
}
/**
* @return string
*/
public function renderData()
{
$response = '';
foreach ($this->_decorators as $decorator) {
$response = $decorator->renderData($this->_data);
}
return $response;
}
}
interface WebserviceDecorator
{
public function renderData($data);
}
class JsonDecorator implements WebserviceDecorator
{
public function renderData($data)
{
return json_encode($data);
}
}
class XmlDecorator implements WebserviceDecorator
{
public function renderData($data)
{
// do some fany conversion to xml from array ...
return simplexml_load_string($data);
}
}
$service = new Webservice(array('foo' => 'bar'));
$service->addDecorator(new JsonDecorator());
echo $service->renderData();