mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-03 13:37:27 +02:00
Proper example of Decorator pattern
Previous example was simply implementing a strategy pattern. A key problem is that WebService made clear that it did no actual output of its own - it handed it off to 'decorators'. A decorator should always wrap a class that already has its own complete, self-contained functionality. In this way, they only enhance / modify it. An instance that is non-decorated should work fully by itself. Note the distinguishing difference between this and sub-classing - here the enhancement is added dynamically, at runtime, and to one instance. With sub-classing, the enhancement is added at compile / parse time, and is applied to all instances / uses of the subclass.
This commit is contained in:
committed by
Dominik Liebler
parent
79c0808858
commit
b8381e5a60
@@ -14,71 +14,61 @@ namespace DesignPatterns;
|
|||||||
* course)
|
* course)
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class Webservice
|
|
||||||
|
interface Renderer
|
||||||
|
{
|
||||||
|
public function renderData();
|
||||||
|
}
|
||||||
|
|
||||||
|
class Webservice implements Renderer
|
||||||
{
|
{
|
||||||
protected $_data;
|
protected $_data;
|
||||||
|
|
||||||
/**
|
|
||||||
* an array to hold 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)
|
public function __construct($data)
|
||||||
{
|
{
|
||||||
$this->_data = $data;
|
$this->_data = $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @param WebserviceDecorator $decorator
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function addDecorator(WebserviceDecorator $decorator)
|
|
||||||
{
|
|
||||||
$this->_decorators[] = $decorator;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function renderData()
|
public function renderData()
|
||||||
{
|
{
|
||||||
$response = '';
|
return $this->_data;
|
||||||
foreach ($this->_decorators as $decorator) {
|
|
||||||
$response = $decorator->renderData($this->_data);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $response;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface WebserviceDecorator
|
abstract class Decorator
|
||||||
{
|
{
|
||||||
public function renderData($data);
|
protected $_wrapped;
|
||||||
}
|
|
||||||
|
|
||||||
class JsonDecorator implements WebserviceDecorator
|
public function __construct($wrappable)
|
||||||
{
|
|
||||||
public function renderData($data)
|
|
||||||
{
|
{
|
||||||
return json_encode($data);
|
$this->_wrapped = $wrappable;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class XmlDecorator implements WebserviceDecorator
|
class RenderInJson extends Decorator implements Renderer
|
||||||
{
|
{
|
||||||
public function renderData($data)
|
public function renderData()
|
||||||
{
|
{
|
||||||
// do some fancy conversion to xml from array ...
|
$output = $this->_wrapped->renderData();
|
||||||
return simplexml_load_string($data);
|
return json_encode($output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class RenderInXml extends Decorator implements Renderer
|
||||||
|
{
|
||||||
|
public function renderData()
|
||||||
|
{
|
||||||
|
$output = $this->_wrapped->renderData();
|
||||||
|
// do some fany conversion to xml from array ...
|
||||||
|
return simplexml_load_string($output);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a normal service
|
||||||
$service = new Webservice(array('foo' => 'bar'));
|
$service = new Webservice(array('foo' => 'bar'));
|
||||||
$service->addDecorator(new JsonDecorator());
|
|
||||||
|
// Wrap service with a JSON decorator for renderers
|
||||||
|
$service = new RenderInJson($service);
|
||||||
|
// Our Renderer will now output JSON instead of an array
|
||||||
|
|
||||||
echo $service->renderData();
|
echo $service->renderData();
|
||||||
|
Reference in New Issue
Block a user