Decorator design pattern is used to enhance the functionality/responsibility of a particular object at run-time or dynamically.
The decorator pattern applies when there is a need to dynamically add as well as remove responsibilities to a class, and when sub-classing would be impossible due to the large number of sub-classes that could result.
Decorator design pattern is based on abstract classes and we derive concrete implementation from those classes.
At the same time other instance of same class will not be affected by this so individual object gets the new behavior.
Implementation of Decorator pattern:
Coffee making scenario
Usage in JDK:
1. All subclasses of java.io.InputStream, OutputStream, Reader and Writer have a constructor taking an instance of same type.
2. java.util.Collections, the checkedXXX(), synchronizedXXX() and unmodifiableXXX() methods.
3. javax.servlet.http.HttpServletRequestWrapper and HttpServletResponseWrapper.
4. Graphical User Interface Frameworks
GUI toolkits use decoration pattern to add functionalities dynamically as explained before.
5. In Mobiquity, to load all ValidationService and MComService and decorate them as business rules(TCP, PIN Check and All).