Inheritance: extends Decorator
Exemplo n.º 1
0
 public static function main()
 {
     $component = new ConcreteComponent();
     $decoratorA = new ConcreteDecoratorA($component);
     $decoratorB = new ConcreteDecoratorB($decoratorA);
     $decoratorA->operate();
     $decoratorB->operate();
 }
class ConcreteDecoratorB extends Decorator
{
    // 具体装饰类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();