07 June 2017

How does Decorator Design Pattern works in java

Decorator design pattern is nothing but adding additional behavior or functionality to existing object without changing its structure. This is a structural Design pattern. This pattern act as wrapper to existing class.

Example: 
1. BufferredReader br = new BufferredReader(new InputStreamReader(new File("C:\input.txt")));
here without changing the behavior of file object it is passed to InputStreamReader and inturn it is passed to BufferredReader object. InputStreamReader is decorator to File object.

2. All wrapper classes we write in java use decorator design pattern.

Points to Remember:
1. Decorator class maintain both Has A and Is A relationship with actual object it is decorating.
2. Decorator class implements the object which it is decorating.
3. Decorator is an object that adds functions to another object.
4. This pattern dynamically changes functionality of an object without changing existing functionality.


Simple UML Diagram:

References:
http://www.oodesign.com/object-pool-pattern.html








No comments:

Post a Comment