interface DecoratorInterface
{
/**
* @return mixed
*/
public function render();
}
class HTMLDecorator implements DecoratorInterface
{
protected $book = null;
public function __construct($book_object)
{
$this->book = $book_object;
}
public function render()
{
$properties = get_object_vars($this->book);
echo '<ul>';
foreach($properties as $key => $value) {
echo '<li>' . $key . ' -- ' . $value . '</li>';
}
echo '</ul>';
}
}
class JSONDecorator implements DecoratorInterface
{
protected $book = null;
public function __construct($book_object)
{
$this->book = $book_object;
}
public function render()
{
echo json_encode($this->book);
}
}
class PatternTest {
public $decorator;
public function __construct(DecoratorInterface $decorator)
{
$this->decorator = $decorator;
}
}
$book = new stdClass();
$book->title = "Patterns of Enterprise Application Architecture";
$book->author_first_name = "Martin";
$book->author_last_name = "Fowler";
echo '<h1>HTML Decorator</h1>';
$htmlDecorator = new HTMLDecorator($book);
$htmlDecorator->render();
echo '<h1>JSON Decorator</h1>';
$jsonDecorator = new JSONDecorator($book);
$jsonDecorator->render();