The problem:

For many use cases, we need to extend the behavior of a type by adding more features to it. These features may be optional and hence it is up to the user to use them.

Solution:

Inheritance does provide a wrapper where some of the code is inherited by the sub type can be made use of. But inheritance results in high coupling. The solution is to allow the users to add wrappers to the objects so as to add new behavior/feature.

Decorator pattern in JDK:

The decorator design pattern is being used in the JDK I/O where one can add buffering, byte/stream/object level streaming etc. The advantage of decorator design pattern is that the same underlying object can be wrapped using multiple wrappers in different ways with each version being different from the other.

Steps to create decorator pattern:

1) Create an interface which is common to all types including wrappers.
2) The addition of such interface will make sure that objects of all classes under decorator pattern are interchangeable.
3) Create a second level base class which implements the above interface to support the wrapper classes.
4) Create a separate wrapper class for each optional feature.
5) Define constructor inside the wrapper claases which accepts an object of type base class

Disadvantage:

The major disadvantage of decorator design pattern is code maintenance as this pattern results in a lot of small objects (decorators). For example for buffering file reader, we need an instance of FileReader and BufferedFileReader classes both of which ultimately extend from Reader class.

Reference

1) http://en.wikipedia.org/wiki/Decorator_pattern
2) http://www.codeproject.com/Articles/42042/Decorator-Design-Pattern
3) http://www.allapplabs.com/java_design_patterns/decorator_pattern.htm

Decorator Design Pattern admin Design Patterns
The problem: For many use cases, we need to extend the behavior of a type by adding more features to it. These features may be optional and hence it is up to the user to use them. Solution: Inheritance does provide a wrapper where some of the code is inherited by the...
<h2>The problem:</h2> <p>For many use cases, we need to extend the behavior of a type by adding more features to it. These features may be optional and hence it is up to the user to use them.</p> <h2>Solution:</h2> <p>Inheritance does provide a wrapper where some of the code is inherited by the sub type can be made use of. But inheritance results in high coupling. The solution is to allow the users to add wrappers to the objects so as to add new behavior/feature.</p> <h2>Decorator pattern in JDK:</h2> <p>The decorator design pattern is being used in the JDK I/O where one can add buffering, byte/stream/object level streaming etc. The advantage of decorator design pattern is that the same underlying object can be wrapped using multiple wrappers in different ways with each version being different from the other.</p> <h2>Steps to create decorator pattern:</h2> <p>1) Create an interface which is common to all types including wrappers. 2) The addition of such interface will make sure that objects of all classes under decorator pattern are interchangeable. 3) Create a second level base class which implements the above interface to support the wrapper classes. 4) Create a separate wrapper class for each optional feature. 5) Define constructor inside the wrapper claases which accepts an object of type base class </p> <h2>Disadvantage:</h2> <p>The major disadvantage of decorator design pattern is code maintenance as this pattern results in a lot of small objects (decorators). For example for buffering file reader, we need an instance of FileReader and BufferedFileReader classes both of which ultimately extend from Reader class.</p> <h2>Reference</h2> <p> 1) http://en.wikipedia.org/wiki/Decorator_pattern 2) http://www.codeproject.com/Articles/42042/Decorator-Design-Pattern 3) http://www.allapplabs.com/java_design_patterns/decorator_pattern.htm </p>