PHP设计模式——装饰器模式

<?php

/**
 * 装饰器模式
 * 如果已有对象的部分内容或功能发生变化,但是不需要修改原始对象的结构,应使用装饰器模式
 * 
 * 为了在不修改对象结构的前提下对现有对象的内容或功能稍加修改,应使用装饰器模式
 */
class Base{
    protected $_content;
    
    public function __construct($content) {
        $this->_content = $content;
    }
    public function edit() {
        return $this->_content;
    }
}

class EditA{
    private $_base = NULL;
    public function __construct(Base $base) {
        $this->_base = $base;
    }
    public function decorator() {
        return $this->_base->edit().'编辑操作A<br>';
    }
}
class EditB{
    private $_base = NULL;
    public function __construct(Base $base) {
        $this->_base = $base;
    }
    public function decorator() {
        return $this->_base->edit().'编辑操作B<br>';
    }
}
//使用
$base = new Base('文章内容...');
$a = new EditA($base);
echo $a->decorator();

$b = new EditB($base);
echo $b->decorator();