Decorator patter is also known as wrapper, the purpose of using this pattern is to attach additional responsibilities to an object dynamically. Sometimes user would want to attach additional responsibilities to an object, there are two ways to go by;
- this first way to add responsibilities to an object is via inheritance
- the second way is to use Decorator pattern
Inheritance would add up responsibilities to every subclass instances, another problem with inheritance is; the choice of border would is made statically rather than dynamic, which is our purpose. [1]
Using decorator pattern would be more feasible as it adapts a flexible approach, decorator encloses the component in another object, the enclosing component is called decorator. [1]Decorator pattern adds responsibilities to an object at run time while sub classing adds responsibilities at compile time. [2]
This pattern is used when;
- user wants to add responsibilities to an object dynamically
- when there is a need of withdrawing responsibilities
References
[1] Design Patterns by GoF
Decorator Pattern
Page - 175 - 184
[2] Decorator Pattern
Available from URL
http://en.wikipedia.org/wiki/Decorator_pattern