首页 > Web开发 > 详细

php设计模式 装饰器模式

时间:2016-03-04 13:16:36      阅读:184      评论:0      收藏:0      [点我收藏+]

装饰器模式,可以动态地添加修改类的功能。

一个类提供了一项功能,如果要修改并添加额外的功能,传统的编程模式需要写一个子类继承它,并重新实现类的方法。使用装饰器模式,仅需要在运行时添加一个装饰器对象即可实现,可以实现最大的灵活性。

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 "<br />\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] = ‘&nbsp;‘; } } } }
ColorDrawDecorator.php
<?php
namespace Baobab;
/**
*修改图形颜色的装饰器
*/
class ColorDrawDecorator implements \Baobab\DrawDecorator{ protected $color; function __construct($color = ‘black‘){ $this->color = $color; } function beforeDraw(){ echo "<div style=‘color: {$this->color};‘>"; }
function afterDraw(){ echo "</div>"; } }
SizeDrawDecorator.php
<?php
namespace Baobab;
/**
*修改图形大小的装饰器
*/
class SizeDrawDecorator implements \Baobab\DrawDecorator{ protected $size; function __construct($size = ‘12px‘){ $this->size = $size; } function beforeDraw() { echo "<div style=‘font-size: {$this->size};‘>"; }
function afterDraw() { echo "</div>"; } }

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();

 

php设计模式 装饰器模式

原文:http://www.cnblogs.com/tianxintian22/p/5241647.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!