Decorator design pattern with an example in Java?

CWC
3 Min Read

Decorator Design Pattern is used for customizing an object’s functionality during its runtime. While this customization happens, there is no impact on same class with other instances and this leads to the user getting the expected behavior. It is a structural design pattern that uses interface with composition and abstract classes (like Composite Pattern, Bridge Pattern, Adapter Pattern etc) to implement.

Composition or inheritance is used to extend behavior object, however it is specifically done during compile time and it applies to all class instances. No new functionality can be removed or added to the current behavior during runtime, so this is where Decorator Design Pattern is useful. For example, if the need is to implement variety of cars, then interface Car can be created so that the assemble method can be defined and this will lead to formation of a basic car which can be eventually extended into a Luxury Car or a Sports Car. However if the need is that, during runtime to the car would have features of a luxury as well as a sports car, then the implementation becomes little complicated. Furthermore, if one wants to follow a chronology while adding features based on their priority and functionality, then the process becomes more challenging. Let’s say, if there are 10 various car types, the logic of implementation using composition and inheritance would not be able to manage to yield optimum results and this is where decorator design pattern in Java comes into play.

Here are the different types that can help to execute a decorator design pattern:

  1. Component Interface: If we consider the aforementioned example of the car designs, then the Car is the component Interface. This means, the abstract class or the interface that would assist in defining the procedures, which would eventually be implemented is the Component Interface.
  2. Component Implementation: Once again, as far as the Car example is considered, Basic Cars is the Component Implementation. So basically it stands for the basic implementation or form of the Component Interface.
  3. Decorator: Decorator class is the one that helps in the implementation of the Component Interface and maintains a HAS-A connection with the same. This is made variable protected to ensure that the child decorator classes has access to the component variable.
  4. Concrete Decorators: When the base decorator function is extended and the component behavior is extended, it leads to the formation of the Concrete Decorators. The Luxury Cars and Sports Cars are considered to be concrete decorator classes, as per the above-mentioned example.

Decorator Design Pattern is easily maintained and extended when the number of options are more. It is used primarily in JAVA IO classes like BufferedReader, FileReader, etc.

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

English
Exit mobile version