装饰器模式

来源:互联网 发布:蹭网器淘宝叫什么 编辑:程序博客网 时间:2024/06/06 16:22

 修饰模式,是面向对象领域中。一种动态的地添加新的行为的设计模式。

<?php    abstract class Component{   abstract public function operation();   }      class MyComponent extends Component{   public function operation(){   echo "这是正常的组件方法";   }   }   abstract class Decotator extends Component{   protected $component;   function __construct(Component $component){   $this->component=$component;   }   public function operation(){   $this->component->operation();   }   }   class MyDecorator extends Decotator{   function __construct(Component $component){   parent::__construct($component);   }   public function addMethod(){   echo "这是装饰器添加的方法";   }   public function operation(){   $this->addMethod();   parent::operation();   }   }   $component=new MyComponent();   $da=new MyDecorator($component);   $da->operation();?>