首页 > 其他 > 详细

Decorator(装饰器模式)

时间:2017-07-04 14:23:56      阅读:279      评论:0      收藏:0      [点我收藏+]

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

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

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

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