This commit is contained in:
Trismegiste
2013-05-11 02:51:57 +02:00
parent 17429eb85a
commit 6b77eb71e6
5 changed files with 13 additions and 6 deletions

View File

@@ -14,17 +14,17 @@ namespace DesignPatterns\Decorator;
* course) * course)
* *
*/ */
/** /**
* the Deoorator MUST implement the Renderer contract, this is the key-feature * the Deoorator MUST implement the Renderer contract, this is the key-feature
* of this design pattern. If not, this is no longer a Decorator but just a dumb * of this design pattern. If not, this is no longer a Decorator but just a dumb
* wrapper. * wrapper.
*/ */
abstract class Decorator implements Renderer abstract class Decorator implements Renderer
{ {
protected $_wrapped; protected $_wrapped;
/** /**
* You must type-hint the wrapped component : * You must type-hint the wrapped component :
* It ensures you can call renderData() in the subclasses ! * It ensures you can call renderData() in the subclasses !
@@ -35,5 +35,5 @@ abstract class Decorator implements Renderer
{ {
$this->_wrapped = $wrappable; $this->_wrapped = $wrappable;
} }
}
}

View File

@@ -4,9 +4,11 @@ namespace DesignPatterns\Decorator;
class RenderInJson extends Decorator class RenderInJson extends Decorator
{ {
public function renderData() public function renderData()
{ {
$output = $this->_wrapped->renderData(); $output = $this->_wrapped->renderData();
return json_encode($output); return json_encode($output);
} }
} }

View File

@@ -4,6 +4,7 @@ namespace DesignPatterns\Decorator;
class RenderInXml extends Decorator class RenderInXml extends Decorator
{ {
public function renderData() public function renderData()
{ {
$output = $this->_wrapped->renderData(); $output = $this->_wrapped->renderData();
@@ -12,7 +13,8 @@ class RenderInXml extends Decorator
foreach ($output as $key => $val) { foreach ($output as $key => $val) {
$doc->appendChild($doc->createElement('foo', 'bar')); $doc->appendChild($doc->createElement('foo', 'bar'));
} }
return $doc->saveXML(); return $doc->saveXML();
} }
} }

View File

@@ -1,8 +1,9 @@
<?php <?php
namespace DesignPatterns\Decorator; namespace DesignPatterns\Decorator;
interface Renderer interface Renderer
{ {
public function renderData(); public function renderData();
} }

View File

@@ -4,6 +4,7 @@ namespace DesignPatterns\Decorator;
class Webservice implements Renderer class Webservice implements Renderer
{ {
protected $_data; protected $_data;
public function __construct($data) public function __construct($data)
@@ -15,4 +16,5 @@ class Webservice implements Renderer
{ {
return $this->_data; return $this->_data;
} }
} }