装饰器模式允许我们根据运行时不同的情景动态地为某个对象调用前后添加不同的行为动作。

<?php
class HtmlTemplate {
// any parent class methods
} class Template1 extends HtmlTemplate {
protected $_html; public function __construct() {
$this->_html = "<p>__text__</p>";
} public function set($html) {
$this->_html = $html;
} public function render() {
echo $this->_html;
}
} class Template2 extends HtmlTemplate {
protected $_element; public function __construct($s) {
$this->_element = $s;
$this->set("<h2>" . $this->_html . "</h2>");
} public function __call($name, $args) {
$this->_element->$name($args[0]);
}
} class Template3 extends HtmlTemplate {
protected $_element; public function __construct($s) {
$this->_element = $s;
$this->set("<u>" . $this->_html . "</u>");
} public function __call($name, $args) {
$this->_element->$name($args[0]);
}
}

  

Decorator(装饰器模式)的更多相关文章

  1. 12、Decorator 装饰器 模式 装饰起来美美哒 结构型设计模式

    1.Decorator模式 装饰模式又名包装(Wrapper)模式.装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 装饰器模式(Decorator Pattern)允许向一个现 ...

  2. Decorator装饰器模式个人理解

    对于装饰器模式,其主要是为了:在不改变本体特征的情况下,对其进行包装.装饰,目的是为了补充.扩展.增强其功能. 有三个原则: 不能改变本体的特征 要对本体的功能进行扩展 装饰器脱离了本体则没有任何含义 ...

  3. 十二、Decorator 装饰器模式

    设计: 代码清单: Display public abstract class Display { public abstract int getColumns(); public abstract ...

  4. java设计模式之七装饰器模式(Decorator)

    顾名思义,装饰模式就是给一个对象增加一些新的功能,而且是动态的,要求装饰对象和被装饰对象实现同一个接口,装饰对象持有被装饰对象的实例,关系图如下: Source类是被装饰类,Decorator类是一个 ...

  5. 说说设计模式~装饰器模式(Decorator)~多功能消息组件的实现

    返回目录 为何要设计多功能消息组件 之前写过一篇装饰器模式的文章,感觉不够深入,这次的例子是实现项目中遇到的,所以把它拿出来,再写写,之前也写过消息组件的文章,主要采用了策略模式实现的,即每个项目可以 ...

  6. 【PHP设计模式 09_ZhuangShiQi.php】装饰器模式 (decorator)

    <?php /** * [装饰器模式 (decorator)] * 有时候发布一篇文章需要经过很多人手,层层处理 */ header("Content-type: text/html; ...

  7. 装饰器模式(Decorator)

    转自http://blog.csdn.net/hust_is_lcd/article/details/7884320 1.认识装饰器模式 装饰模式能够实现动态的为对象添加功能,是从一个对象外部来给对象 ...

  8. 设计模式(八)装饰器模式Decorator(结构型)

    设计模式(八)装饰器模式Decorator(结构型) 1. 概述 若你从事过面向对象开发,实现给一个类或对象增加行为,使用继承机制,这是所有面向对象语言的一个基本特性.如果已经存在的一个类缺少某些方法 ...

  9. 设计模式入门之装饰器模式Decorator

    //装饰模式定义:动态地给一个对象加入一些额外的职责. //就添加功能来说.装饰模式比生成子类更为灵活 //这也提现了面向对象设计中的一条基本原则,即:尽量使用对象组合,而不是对象继承 //Compo ...

随机推荐

  1. apache安装报错

    libtool: install: error: cannot install `libaprutil-1.la' to a directory not ending /some_directory ...

  2. Git常用命令的使用方法

    推荐一个比较好的GIT的教学地址,廖雪峰老师的git教程! 这里简述Git常用命令的使用方法: 一.初始化git 右键进入 Git Bash 1.建立身份信息 git config --global ...

  3. Selenium私房菜系列9 -- Selenium RC服务器命令行参数列表【ZZ】

    本文转载自:http://wiki.javascud.org/display/SEL/Selenium+Remote+Control+-+options 使用示例: java -jar seleniu ...

  4. SnowKiting

    原文 Let's go fly a kite...in the snow Reach into your closet,find that dusty kite and clean it off - ...

  5. SAP产品的Field Extensibility

    SAP开发人员的工作职责,除了实现软件的功能性需求外,还会花费相当的精力实现一些非功能性需求,来满足所谓的SAP Product Standard(产品标准).这些产品标准,包含在SAP项目实施中大显 ...

  6. 如何删除github上项目的文件

    1. 你要有前面一章的开发平台和github插件,下面就是基于前面来做的. 如何删掉你github上的文件呢?想必你的电脑有一个下载的git工具了,如果还是没有的话,请用npm下载一个git.这是我已 ...

  7. 集成iAd广告

    在iPhone程序中集成广告,管他能不能赚钱,不放上一个iAd就心有不甘. 参考了下面这篇文章: http://bees4honey.com/blog/tutorial/how-to-add-iad- ...

  8. poj1338 Ugly Numbers 打表, 递推

    题意:一个数的质因子能是2, 3, 5, 那么这个数是丑数. 思路: 打表或者递推. 打表: 若该数为丑数,那么一定能被2 或者3, 或者5 整除, 除完之后则为1. #include <ios ...

  9. Django 模型ORM

    from django.db import models # Create your models here. class Book(models.Model): nid = models.AutoF ...

  10. 系统学习爬虫_2_urllib

    什么是urllib urlopen urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cad ...