码迷,mamicode.com
首页 > 其他好文 > 详细

Decorator(装饰器模式)

时间:2017-07-04 14:59:40      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:blog   htm   ons   method   render   允许   装饰器   logs   cal   

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

<?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(装饰器模式)

标签:blog   htm   ons   method   render   允许   装饰器   logs   cal   

原文地址:http://www.cnblogs.com/Czc963239044/p/7116066.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!