Skip to end of metadata
Go to start of metadata

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:

  1. Create a Subclass of the main class, also called a component, as a Decorator class.
  2. In decorator class, create an attribute with TYPE REF TO main class
  3. Create CONSTRUCTOR in the decorator class with importing parameter REF TO Decorator class. Set the attribute from this parameter.
  4. Redefine all other methods in decorator class inherited from Main Class. Just call the same method using the reference of the main class.
  5. Create subclass of decorator when a new behaviour is required. Redefine the methods for which we need different behavior.

More information

1 Comment

  1. Former Member

    Thanks Naimesh, for the detailed example.