装饰模式
2015-06-27 17:18
253 查看
/** * 规定装饰类与被装饰类都必须实现的方法 */ interface component { function operation(); } /** * 被装饰的类 */ class concrete_component implements component { public function operation() { echo '具体履行职责的对象<br/>'; } } /** * 装饰器抽象类 */ abstract class decorator_component implements component { protected $component; /** * 添加装饰器方法 */ public function set_component (component $component) { $this->component = $component; } public function operation() { if( $this->component != null ) { $this->component->operation(); } } } /** * 第一个装饰器 */ class decorator_a extends decorator_component{ public function operation() { parent::operation(); echo 'decorator_a<br/>'; } } /** * 第二个装饰器 */ class decorator_b extends decorator_component{ public function operation() { parent::operation(); echo 'decorator_b<br/>'; } } $concrete_component = new concrete_component(); $decorator_a = new decorator_a(); $decorator_a->set_component($concrete_component); $decorator_b = new decorator_b(); $decorator_b->set_component($decorator_a); $decorator_b->operation();
装饰模式(decorator):动态的给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。
相关文章推荐
- Windows 下音频数据采集和播放(转)
- Html去除缓存
- 归并排序 & 计数排序 & 基数排序 & 冒泡排序 & 选择排序 ----> 内部排序性能比较
- 从贫穷到富有——我的修行之路
- MD5加密
- Android学习之天气预报简单版
- Eclipse加入PHP插件并支持PHP
- setContentView剖析
- c++ 继承相关注意点
- Android---61---TabHost简单使用
- C#Winform控件随窗体缩放
- Android中数据库的操作流程详细解释
- (转) ThinkPHP模板自定义标签使用方法
- 世界,您好!
- 2.30
- 把一个表的某个字段值改写到另外一张表的某个字段
- UILabel属性使用
- Apache/Tomcat/JBOSS/Nginx区别
- Unity物理引擎研究之关节
- uploadify"ID SWFUpload_0 is already in use..."错误的解决方法