class Window { use TDecorator; private $title; function __construct($title) { $this->title = $title; } function draw() { // Draws window below // ... // Draws decorations. $this->renderDecoration('draw'); } } class VerticalScrollbar { use TDecoration; private $vWidth; private $vHeight; function __construct($width, $height) { $this->vWidth = $width; $this->vHeight = $height; } function draw() { echo 'Drawing vertical scrollbar for the window "' . $this->title . "\"\n"; echo 'Width: ' . $this->vWidth . "px\n"; echo 'Height: ' . $this->vHeight . "px\n"; } } // Decorates window with a vertical scrollbar. $wnd = new Window('My application'); $wnd->decorateWithObject(new VerticalScrollbar(20, 20)); $wnd->draw();
class Integer { use TDecorator; protected $value; function __construct($value) { $this->value = intval($value); } } $integer = new Integer(9); $integer->decorateWith('isOdd', function() { return (boolean) ($this->value % 2); }); echo $integer->isOdd(); // echoes true
Source: https://habr.com/ru/post/166237/
All Articles