#4 Design Patterns — Decorator Pattern

Hello Everyone, Welcome to my blog 🙏.
I hope you are all safe and that this blog finds you in good health ❤️.

In my previous blogs, we have learnt about
1. What are design patterns and What is Observer Pattern?
2. What is Constructor Pattern?
3. What is Prototype Pattern?

In this blog, we will learn more about
👉 Decorator Pattern

Decorator Pattern

We know that the Prototype pattern shares the properties among other objects of the same type but, the Decorator pattern adds new properties to individual objects without affecting the other objects of the same type.

How does Decorator Pattern work?
To understand it better, Let us take an example

In my previous blogs, we know that Steve had a boutique, where he manufactures high-quality shirts and also customers like his collections and purchase them.
He imports High-quality fabrics from different countries which is a primary reason for his successful business. In his manufacturing factory, everything is automated with different machinery. For all his machines, there is a common
process followed to get similar shirts.
And also we have seen how the constructor pattern works.

Adding to that, Steve wants to upgrade his machines. He wants them to pack the shirts along with stitching.
He succeeded in doing so with the help of the prototype pattern.

Now that Steve’s business is in good shape, he also wants to manufacture custom design shirts ordered by his customers like printing the name on the shirts, autographs of famous celebrities, ethnic wear, etc. As he didn’t want to upgrade this process to all his machines instead, he wants a few of them to be upgraded.

Now, that we have received the requirement from Steve, let’s see how the Decorator pattern helps us here.


Functional-based approach

As you have noticed we get type error if machine2 tried to access customDesign method which clearly states customDesign method is only applicable for machine3 and it didn’t disturb the existing ManufactureProcess.
This acts similarly to the class-based approach as well.


Class-based approach

Now, Steve is ready for announcing custom design shirt manufacturing that added great value to his collections in the market.

Thanks to Decorator Pattern 🎯.

When to use the Decorator pattern?

The Decorator pattern is used in various applications which require role-specific access for the application, for example, HR and employees although they use the same application HR can view the salaries of other employees whereas employees can’t.
Simply, It can be used wherever we can modify or add new features to the existing objects without disturbing them.

Here we go, That’s it folks for this blog. Hope everyone liked it.
If you like it, give it a clap 👏 ,
and share it with your friend.

For more exciting content on Frontend, Please do follow me 🎯.

Thanks a lot, Everyone 🙏.
Until Next time,
Happy Learning ✍️

Abhishek Kovuri, UI developer




UI developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store