The person who is the decorator base class Interface Component { Public function operation (); } Decorator base class Abstract class Decorator implements Component { protected $component; Public function __construct (Component $component) { $this->component = $component; } Public Function operation () { $this->component->operation (); } } Concrete Decorator Class Class Concretecomponent implements Component { Public Function operation () { Echo ' do operation '. Php_eol; } } Concrete Decoration Class A Class Concretedecoratora extends Decorator { Public function __construct (Component $component) { Parent::__construct ($component); } Public function operation () { Parent::operation (); $this->addedoperationa (); Newly added operations } Public Function Addedoperationa () { Echo ' Add operation A '. Php_eol; } } Concrete Decoration Class B Class Concretedecoratorb extends Decorator { Public function __construct (Component $component) { Parent::__construct ($component); } Public function operation () { Parent::operation (); $this->addedoperationb (); } Public Function addedoperationb () { Echo ' Add operation B '. Php_eol; } } Class Client { public static function main () { /* Do operation ADD Operation A */ $decoratorA = new Concretedecoratora (new Concretecomponent ()); $decoratorA->operation (); /* Do operation ADD Operation A ADD Operation B */ $decoratorB = new Concretedecoratorb ($decoratorA); $decoratorB->operation (); } } Client::main (); |