Created
August 27, 2014 08:04
-
-
Save sokil/b1351c09c2866bd59cca to your computer and use it in GitHub Desktop.
PHP Decorator pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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