Decorator pattern gives us flexibility to extend the behavior of certain objects at runtime. This can be seperate than the existing instances of the same class. To be able to achieve Decorator pattern, we need to create certain infrastructure for it.
Design time consideration:
- Create a Subclass of the main class, also called a component, as a Decorator class.
- In decorator class, create an attribute with TYPE REF TO main class
- Create CONSTRUCTOR in the decorator class with importing parameter REF TO Decorator class. Set the attribute from this parameter.
- Redefine all other methods in decorator class inherited from Main Class. Just call the same method using the reference of the main class.
- Create subclass of decorator when a new behaviour is required. Redefine the methods for which we need different behavior.