Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>there is a solution to fit the coffeemachine problem</p> <pre><code>abstract class Coffee { protected $cup = array(); public function getCup() { return $this-&gt;cup; } } class SimpleCoffee extends Coffee { public function __construct() { $this-&gt;cup['coffeePowder'] = 1; $this-&gt;cup['water'] = 20; $this-&gt;cup['spoon'] = FALSE; } } abstract class Decorator extends Coffee { private $_handler = null; public function __construct($handler) { $this-&gt;_handler = $handler; $this-&gt;cup = $handler-&gt;cup; } } class SugarCoffee extends Decorator { public function __construct($handler) { parent::__construct($handler); $this-&gt;cup['water'] -= 1; $this-&gt;cup['sugar'] = 1; $this-&gt;cup['spoon'] = TRUE; } } class MilkCoffee extends Decorator{ public function __construct($handler) { parent::__construct($handler); $this-&gt;cup['water'] -= 5; $this-&gt;cup['milk'] = 5; } } $wantSugar = TRUE; $wantMilk = TRUE; $coffee = new SimpleCoffee(); if($wantSugar) $coffee = new SugarCoffee($coffee); if($wantMilk) $coffee = new MilkCoffee($coffee); $cupOfCoffee = $coffee-&gt;getCup(); var_dump($cupOfCoffee); </code></pre> <p>and there is another <code>real world example</code>, I hope it can help you:</p> <pre><code>abstract class MessageBoardHandler { public function __construct(){} abstract public function filter($msg); } class MessageBoard extends MessageBoardHandler { public function filter($msg) { return "added in messageBoard|".$msg; } } class MessageBoardDecorator extends MessageBoardHandler { private $_handler = null; public function __construct($handler) { parent::__construct(); $this-&gt;_handler = $handler; } public function filter($msg) { return $this-&gt;_handler-&gt;filter($msg); } } class HtmlFilter extends MessageBoardDecorator { public function __construct($handler) { parent::__construct($handler); } public function filter($msg) { return "added in html filter|".parent::filter($msg); } } class SensitiveFilter extends MessageBoardDecorator { public function __construct($handler) { parent::__construct($handler); } public function filter($msg) { return "added in sensitive filter|".parent::filter($msg); } } $html = TRUE; $sencitive = TRUE; $obj = new MessageBoard(); if($html) { $obj = new SensitiveFilter($obj); } if($sencitive) { $obj = new HtmlFilter($obj); } echo $obj-&gt;filter("message"); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload