|
// Decorator base class
Interface Component
{
Public function operation ();
}
// Modifier base class
Abstract class Decorator implements Component
{
Protected $ component;
Public function _ construct (Component $ component)
{
$ This-> component = $ component;
}
Public function operation ()
{
$ This-> component-> operation ();
}
}
// Specific modifier class
Class ConcreteComponent implements Component
{
Public function operation ()
{
Echo 'do operation'. PHP_EOL;
}
}
// Specific decoration Class
Class ConcreteDecoratorA extends Decorator {
Public function _ construct (Component $ component ){
Parent: :__ construct ($ component );
}
Public function operation (){
Parent: operation ();
$ This-> addedOperationA (); // Newly added operation
}
Public function addedOperationA (){
Echo 'add Operation a'. PHP_EOL;
}
}
// Specific 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
*/
$ DecoratorA = new ConcreteDecoratorA (new ConcreteComponent ());
$ DecoratorA-> operation ();
/*
Do operation
Add Operation
Add Operation B
*/
$ DecoratorB = new ConcreteDecoratorB ($ decoratorA );
$ DecoratorB-> operation ();
}
}
Client: main ();
|