2011-08-26 19:29:16 +02:00
|
|
|
<?php
|
|
|
|
|
2013-05-11 02:07:29 +02:00
|
|
|
namespace DesignPatterns\Decorator;
|
2011-08-26 19:29:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-05-11 02:51:57 +02:00
|
|
|
/**
|
2013-09-11 16:18:40 +02:00
|
|
|
* the Deoorator MUST implement the RendererInterface contract, this is the key-feature
|
2013-05-11 02:06:15 +02:00
|
|
|
* of this design pattern. If not, this is no longer a Decorator but just a dumb
|
|
|
|
* wrapper.
|
|
|
|
*/
|
2013-09-11 16:18:40 +02:00
|
|
|
abstract class Decorator implements RendererInterface
|
2013-05-08 21:30:09 +10:00
|
|
|
{
|
2013-09-11 16:18:40 +02:00
|
|
|
/**
|
|
|
|
* @var RendererInterface
|
|
|
|
*/
|
|
|
|
protected $wrapped;
|
2011-08-26 19:29:16 +02:00
|
|
|
|
2013-05-11 02:06:15 +02:00
|
|
|
/**
|
|
|
|
* You must type-hint the wrapped component :
|
|
|
|
* It ensures you can call renderData() in the subclasses !
|
|
|
|
*
|
2013-09-11 16:18:40 +02:00
|
|
|
* @param RendererInterface $wrappable
|
2013-05-11 02:06:15 +02:00
|
|
|
*/
|
2013-09-11 16:18:40 +02:00
|
|
|
public function __construct(RendererInterface $wrappable)
|
2011-08-26 19:29:16 +02:00
|
|
|
{
|
2013-09-11 16:18:40 +02:00
|
|
|
$this->wrapped = $wrappable;
|
2011-08-26 19:29:16 +02:00
|
|
|
}
|
|
|
|
}
|