Two different ways to implement Decorator pattern in PHP -
while doing tutorials on decorator pattern, i've encountered 2 different implementations.
implementation 1 (referred i1)
implementation 2 (referred i2)
in short,
i1's parent decorator class implements original object's interface (in example, class propertydecorator
implements propertyinterface
. original object property
implements propertyinterface
well).
i2's parent decorator class not implement original object's interface (in example, decorator_wrapper
not implements cupcake
interface. in fact, there not cupcakeinterface
@ all).
my question is,
is merely personal preference of understanding , implementing decorator pattern? or 1 wrong , 1 right?
just depends on needs. let's see:
abstract class
- can provide abstract methods.
- can provide real functions , variables.
- can extended. class can extend 1 parent.
interface
- can provide abstract methods.
- a class may implement several interfaces.
i prefer using base abstract class, because can declare basic functions well, since have lot of different types of decorators similar functionality. methods can overriden anyways + can implement interfaces.
class decorator extends decorator_wrapper implements interface1, inteface2 { public function __construct(){ parent::__construct() ; // here perform basic decorator actions. advantage compared interfaces. } }
Comments
Post a Comment