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
8 comments:
If I am not wrong, Adapter Pattern is also known as wrapper. So what is the relation between Adapter Pattern and Decorator Pattern ?
sorry for late reply :(
well wraper is the name used for adapter and decorator because of their functionality.
Adapter wraps iself around one class and makes it compatible with other class that's why it is called wrapper.
On the other hand Decorator pattern adds additional responsibilities to a class and it encloses the component in another object thus it is also called wrapper.
hope answer to the question
Hi sorry for jumping in.
I think Adapter Design pattern is more of converting the interface to a common platform to make incompatiable interfaces work.
Decorator Design Pattern is also a wrapper but it now wraps additional responsibilities around the object instead of converting interfaces.
I hope its not confusing.
Goodday.
Yeah i think giving more jobs to an object to perform is a good technique but dont you think it may put more burden on the memory? when any program is executed ! ..
Your going off the track hani lol but anyways coming back to your question. Tell mw how much memory one java program takes to execute, its in Kbs, right. Now by adding more responsibilities to an object, it would just add couple of Kbs, dont forget that nowadays we get RAMs that are in GBs so personally i dnt think it would be an issue.
hope answer to the question
Well said that one way to duplicate decorator is to add responsibilities to an object is via inheritance. Although inheritance has its advantages, the decorator logic is tightly coupled to the class and can not be re-used
hi abbas...
quite interesting on your thoughts on decorator although,
when reading this blog i thisnk you got mistaken with purpose of the pattern and apllication.
BTW guys, I just noticed that I have made a mistake in this post, I didnt put the whole example from the book :(
"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]"
This example is not complete...
In reply to your question Hanish, I didnt exactly get what you mean, can you be a bit more elaborate, then i will answer your question
thankss
Post a Comment