php设计模式 装饰器模式 - tianxintian22
程序员文章站
2024-01-13 15:14:10
...
装饰器模式,可以动态地添加修改类的功能。
一个类提供了一项功能,如果要修改并添加额外的功能,传统的编程模式需要写一个子类继承它,并重新实现类的方法。使用装饰器模式,仅需要在运行时添加一个装饰器对象即可实现,可以实现最大的灵活性。
DrawDecorator.php
php namespace Baobab; /** * Interface DrawDecorator * @package Baobab * 装饰器接口,定义两个方法,渲染画布之前和之后可添加额外的能力 */ interface DrawDecorator{ function beforeDraw(); function afterDraw(); }
Canvas.php
php namespace Baobab;
/**
*Canvas类,绘制图形
**/
class Canvas { public $data; protected $decorators = array(); //Decorator 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; } function addDecorator(DrawDecorator $decorator){ $this->decorators[] = $decorator; } function beforeDraw(){ foreach($this->decorators as $decorator){ $decorator->beforeDraw(); } } function afterDraw(){ $decorators = array_reverse($this->decorators); foreach($decorators as $decorator){ $decorator->afterDraw(); } } function draw() { $this->beforeDraw(); foreach($this->data as $line) { foreach($line as $char) { echo $char; } echo "
\n"; } $this->afterDraw(); } 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] = ' '; } } } }
ColorDrawDecorator.php
php namespace Baobab; /**
*修改图形颜色的装饰器
*/ class ColorDrawDecorator implements \Baobab\DrawDecorator{ protected $color; function __construct($color = 'black'){ $this->color = $color; } function beforeDraw(){ echo ""; }"; } }
function afterDraw(){ echo "
SizeDrawDecorator.php
php namespace Baobab; /**
*修改图形大小的装饰器
*/ class SizeDrawDecorator implements \Baobab\DrawDecorator{ protected $size; function __construct($size = '12px'){ $this->size = $size; } function beforeDraw() { echo ""; }"; } }
function afterDraw() { echo "
index.php
$canvas = new Baobab\Canvas(); $canvas->init(); $canvas->rect(3, 6,4,12); $canvas->addDecorator(new \Baobab\ColorDrawDecorator('blue')); $canvas->addDecorator(new \Baobab\SizeDrawDecorator('10px'));
......
//可添加其他更多装饰器
$canvas->draw();