Skip to content

Instantly share code, notes, and snippets.

@sokil
Created August 27, 2014 08:04
Show Gist options
  • Save sokil/b1351c09c2866bd59cca to your computer and use it in GitHub Desktop.
Save sokil/b1351c09c2866bd59cca to your computer and use it in GitHub Desktop.
PHP Decorator pattern
<?php
abstract class AbstractWindow
{
protected $_title;
public function __construct($title = null)
{
$this->_title = $title;
}
abstract public function draw();
}
class LinuxWindow extends AbstractWindow
{
public function draw()
{
return 'Linux Window "' . $this->_title . '"';
}
}
class OsxWindow extends AbstractWindow
{
public function draw()
{
return 'OsX Window "' . $this->_title . '"';
}
}
abstract class AbstractWindowDecorator extends AbstractWindow
{
protected $_window;
public function __construct(AbstractWindow $window)
{
$this->_window = $window;
}
}
class BorderedWindow extends AbstractWindowDecorator
{
public function draw()
{
return 'Bordered ' . $this->_window->draw();
}
}
class TransparentWindow extends AbstractWindowDecorator
{
public function draw()
{
return 'Transparent ' . $this->_window->draw();
}
}
$window = new TransparentWindow(new BorderedWindow(new LinuxWindow("About...")));
echo $window->draw();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment