Decorator Design Pattern

Decorator Design Pattern

Decorator / Wrapper is one of the most heavily used structural design pattern. You might have also come across it many times. In this article we will understand what is Decorator pattern and how it's implemented.

Consider that you are working on an image sharing application that reads from and writes images to the storage.

This is what the code looks like:

No alt text provided for this image

However later on you also want to give your users additional features like encryption/decryption, compression/decompression, renaming, beautification etc.

Now you can extend the image class to create these different types of images and write logic to perform these manipulations in the extended classes. Then depending upon the type of image (compressed, cropped, encrypted etc.) you can choose one of these extended classes. But some users might want to do multiple of these operations together - compression with encryption or maybe beautification, compression and cropping together etc. Now you'll have to create classes for all different combinations of these actions (image below). Soon, the number of classes created would explode. Addition of any new actions would mean a LOT of developer efforts.

No alt text provided for this image

What now?

We can make use of the Decorator pattern which suggests that we create a class and make it extend the wrappee, image here (or the parent interface of wrappee). Now our class can be used as an wrapee (image). Next we aggregate the wrappee object (or the parent interface of wrappee) inside our class. Our class then performs the required operation and then delegates the saving/retrieval logic to the parent class.

Here is what the implementation looks like

No alt text provided for this image

We can see that this saves us a lot of developer efforts. Different decorators can also be plugged in at any time in the code without any required modifications in the code. This makes the code open for extension but closed for modification. The decorators can also be chained without the need to create any new classes. This pattern also breaks responsibility to multiple classes compared to one big class handling everything.

I took help from the following sources to write this article:

  1. https://refactoring.guru/design-patterns/decorator
  2. https://www.tutorialspoint.com/design_pattern/decorator_pattern.htm
  3. https://www.geeksforgeeks.org/decorator-design-pattern-in-java-with-example/t/

Hope you liked this article. I regularly write such short articles on LinkedIn. Follow Prateek Mishra and learn something new everyweek.

To view or add a comment, sign in

More articles by Prateek Mishra

  • Performance Testing: An Overview

    Performance testing is a crucial aspect of software development that ensures the quality and reliability of your…

    2 Comments
  • Builder Design Pattern

    The builder pattern is a popular design pattern that is used to separate the construction of an object from its…

    5 Comments
  • Memento Design Pattern

    Memento design pattern is used to save the state of an object at any point of time and then later go back to the older…

  • Iterator Design Pattern

    Iterator design pattern is one of the most used design patterns. In this article we will discuss the what, why and how…

  • Command Design Pattern

    Command Pattern is one of the Behavioral Design Patterns which focuses on how objects and classes should communicate…

  • Chain of Responsibility Pattern

    Consider that you need an allowance for your team and for that you need to take budget approval from your manager…

    1 Comment
  • Flyweight Design Pattern

    Flyweight Pattern is a Structural Design Pattern which can be used to save memory by sharing fields between objects…

    1 Comment
  • Composite Design Pattern

    Corporate : Michael, we are having an audit. We need your branch's revenue data by the end of the day.

  • Bridge Design Pattern

    Bridge pattern is one of the Structural Design Patterns. Consider the problem given in the image below where there is…

  • Adapter Design Pattern

    An adapter is, like we all know, a bridge that facilitates communication between two incompatible interfaces. Adapter…

Others also viewed

Explore content categories