{ private $addedState; //本类都有的,以区别于ConcreteDecoratorB public function operation() { parent::operation(); $this->addedState = 'New state'; var_dump('具体装饰对象A的操作'); } } /* * 具体装饰类A */ class ConcreteDecoratorB extends Decorator { public function operation() { parent::operation(); $this->Addbehavior(); var_dump('具体装饰对象B的操作'); } public function Addbehavior() { } } //客户端代码 $c = new ConcreteComponent(); $d1 = new ConcreteDecoratorA($c); $d1 = new ConcreteDecoratorB($d1); $d1->operation();
{ // 具体装饰类B public function __construct(Component $component) { parent::__construct($component); } public function operation() { parent::operation(); $this->addedOperationB(); } public function addedOperationB() { echo "B加点辣椒;"; } } class ConcreteComponent implements Component { //具体组件类 public function operation() { } } // clients $component = new ConcreteComponent(); $decoratorA = new ConcreteDecoratorA($component); $decoratorB = new ConcreteDecoratorB($decoratorA); $decoratorA->operation(); echo '<br>--------<br>'; $decoratorB->operation();