## 装饰器模式 * 1、装饰器模式( Decorator),可以动态地添加修改类的功能 * 2、一个类提供了一项功能,如果要在修并添加额外的功能,传统的编程模式,需要写一个子类继承它,并重新实现类的方法 * 3、使用装饰器模式,仅需在运行时添加一装饰器对象即可实现,可以实现最大的灵活性 > 装饰器类 `Canvas.php` ``` class Canvas { public $data; /** * 所有添加过的装饰器 * @var array */ protected $decorators = array(); // Decorator public function init($width = 20, $height = 10) { $data = array(); for ($i = 0; $i < $height; $i++) { for ($j = 0; $j < $width; $j++) { $data[$i][$j] = '*'; } } $this->data = $data; } /** * 动态添加装饰器对象 * @param DrawDecorator $decorator */ public function addDecorator(DrawDecorator $decorator) { $this->decorators[] = $decorator; } /** * 先进先出 */ public function beforeDraw() { // 循环去调用没有装饰器的方法 foreach ($this->decorators as $decorator) { $decorator->beforeDraw(); } } /** * 后进先出 */ public function afterDraw() { // 翻转 $decorators = array_reverse($this->decorators); foreach ($decorators as $decorator) { $decorator->afterDraw(); } } public function draw() { $this->beforeDraw(); foreach ($this->data as $line) { foreach ($line as $char) { echo $char; } echo "<br />\n"; } $this->afterDraw(); } public function rect($a1, $a2, $b1, $b2) { foreach ($this->data as $k1 => $line) { if ($k1 < $a1 or $k1 > $a2) continue; foreach ($line as $k2 => $char) { if ($k2 < $b1 or $k2 > $b2) continue; $this->data[$k1][$k2] = '&nbsp;'; } } } } ``` > 装饰器接口 `DrawDecorator.php` ``` interface DrawDecorator { public function beforeDraw(); public function afterDraw(); } ``` > 颜色装饰器 `ColorDrawDecorator.php` ``` class ColorDrawDecorator implements DrawDecorator { protected $color; public function __construct($color = 'red') { $this->color = $color; } public function beforeDraw() { echo "<div style='color: {$this->color};'>"; } public function afterDraw() { echo "</div>"; } } ``` > 字体装饰器`SizeDrawDecorator.php` ``` class SizeDrawDecorator implements DrawDecorator { protected $size; public function __construct($size = '14px') { $this->size = $size; } public function beforeDraw() { echo "<div style='font-size: {$this->size};'>"; } public function afterDraw() { echo "</div>"; } } ``` > 实现类 ``` public function CanvasDemo() { $canvas = new Canvas(); $canvas->init(); $canvas->rect(3,6,4,12); $canvas->addDecorator(new ColorDrawDecorator('green')); $canvas->addDecorator(new SizeDrawDecorator('40px')); $canvas->draw(); } ```