The decorator design pattern is used to enhance the functionality/responsibility of a particular object at run-time or dynamically. It 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 a large number of sub-classes that could result.
The decorator design pattern is based on abstract classes and we derive concrete implementation from those classes.
At the same time, other instance of the same class will not be affected by this so individual object gets the new behavior.
Implementation of the Decorator pattern
Coffee making scenario
Usage in Java Libraries
1. All subclasses of java.io.InputStream, OutputStream, Reader, and Writer have a constructor taking an instance of the 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).