When i was studying about the decorator pattern further,i have realised lot of valuble things are included in pattern language for software developers.
Another important fact about decorator pattern is,it shows to add responsibilites to individual objects dynamically without affecting other objects.
The following example will show how it happen.
This example is based on the windows system.We assume windows are representing by instances of widows class .To allow scrolling the windows form,must contain horizonal or vertical scrollbars.The windows class has no functionality for adding scrollbars and borders.Using decorator pattern we use subclass called decorator to add functionality to existing windows object.According to following diagram,textview class and decorator are subclasses of the windows.
As i explained earlier it will add responsibility to individual object without affecting or changing any other objects.

Why we cannot use Inheritance?
As above diagram,suppose we have a text view GUI component and it would be added with different kind of borders and scrollbars to complete windows form. There are two borders called plain borders and 3Dborders and two scrollbars called vertical and horizonal.The borders and scrollbars are subclasses of decorator.
Using the inheritance we need to make lot of subclasses to represent text view for all cases.
example-text view-plain
-text view-plain vertical
-text view-3D
-text view-3D horizanal
Without making more subclases ,we can use decorator pattern to mix and match decorators any way we like.Decorate pattern has same super type as the object they decorate.We can use one or more objects to wrap an object.
Related Patterns
Strategy
The decorator pattern allow variable behavior before or after the properties,functions,of another object are called.If developer wants to arrang for things to happen in the middle of calls to properties,funcitons or subroutines can use strategy pattern.
Template Method
The template method allow variable behavior in the middle fo a function or subroutine call instead of before or after it.The decorator
Filter
The filter pattern is focus on manupulating a data stream.Its a specialised version of the decorator.
References
Visual Basic Design Patterns / Mark Grand, Brad Merrill / Wiley Publishing inc /ISBN - 81-265-0614-8
No comments:
Post a Comment