Decorator Design Pattern

Hands-on implementation of the popular design pattern.Photo by Fotis Fotopoulos on UnsplashLet’s begin with a conceptual diagram, the pictorial representation will help us to understand the flow.Conceptual diagram of decorator design patternClass Decor…

Hands-on implementation of the popular design pattern.

Photo by Fotis Fotopoulos on Unsplash

Let’s begin with a conceptual diagram, the pictorial representation will help us to understand the flow.

Conceptual diagram of decorator design pattern

Class DecoratorOne is injecting an IComponent interface. This allows you to inject any behavior you’re looking to add, for example, passing the object that you wish to decorate.

So DecoratorOne is acting as a wrapper around decoration. In the above diagram, you can see the Decorator class keeps the reference to the object being decorated. In this case, a component class.

Not only our DecoratorOne class IS-A IComponent but also its HAS-A IComponent. This is because the decorator class implements the same interface as the original Component class, so it now has a chance to intercept any method calls on the interface and inject some additional behavior into those calls.

Let’s order a dinner for an example.

  1. Order a pizza: interface Ipizza
  2. Dinner : class Dinner: Ipizza
  3. Add Pepsi: class Dinner: IPepsi
  4. Add Pasta: class Dinner: IPasta

In the end, we ended up ordering a dinner with pizza, pepsi and pasta. We kept injecting our new requirements and kept decorating old classes to get us something additional.

So when you hear the word decorator, what you’re really doing is designing a series of objects that can wrap around each other and inject behavior as needed.

You can, in fact, define and use multiple decorator objects together so you can have a layered structure around your original object, much like an onion.

Each decorator class can define different behaviors, so you maintain that strong separation of concerns.

The notion of decorator design pattern

Since all of your decorator classes implement the same interface as your original object, you don’t have to change any of your client code to add-in these various new behaviours.

Let’s begin with code and implement the following design:

An example of a smartphone

Let’s add interface IMobile:

Next, add another interface IAndroid:

Now for our default implementation of Android class, let’s return some strings & values through our functions.

At last, We need a decorator class, add additional information to functions.

The observer overridden methods are adding additional information to the injected class, hence it’s a IS-A & HAS-A relationship.

Let’s go ahead and call it through the program.

Run the app and see the magic.

Hurray!!!

As you can see, it is a combination of both objects. One default implementation & another decorating default object.

Being able to layer objects together in this layered structure and then intercept and modify method calls is a very powerful idea because it allows us to separate concerns and dynamically add new functionality when new needs come up. So, as you work with the decorator pattern, keep these ideas in mind.

I hope you gained something out of this article and I wish you good luck.

Cheers!!

Rikam Palkar – Software Engineer – CygNet – Weatherford | LinkedIn


Decorator Design Pattern was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


Print Share Comment Cite Upload Translate
APA
Rikam Palkar | Sciencx (2024-03-29T11:10:30+00:00) » Decorator Design Pattern. Retrieved from https://www.scien.cx/2022/02/28/decorator-design-pattern/.
MLA
" » Decorator Design Pattern." Rikam Palkar | Sciencx - Monday February 28, 2022, https://www.scien.cx/2022/02/28/decorator-design-pattern/
HARVARD
Rikam Palkar | Sciencx Monday February 28, 2022 » Decorator Design Pattern., viewed 2024-03-29T11:10:30+00:00,<https://www.scien.cx/2022/02/28/decorator-design-pattern/>
VANCOUVER
Rikam Palkar | Sciencx - » Decorator Design Pattern. [Internet]. [Accessed 2024-03-29T11:10:30+00:00]. Available from: https://www.scien.cx/2022/02/28/decorator-design-pattern/
CHICAGO
" » Decorator Design Pattern." Rikam Palkar | Sciencx - Accessed 2024-03-29T11:10:30+00:00. https://www.scien.cx/2022/02/28/decorator-design-pattern/
IEEE
" » Decorator Design Pattern." Rikam Palkar | Sciencx [Online]. Available: https://www.scien.cx/2022/02/28/decorator-design-pattern/. [Accessed: 2024-03-29T11:10:30+00:00]
rf:citation
» Decorator Design Pattern | Rikam Palkar | Sciencx | https://www.scien.cx/2022/02/28/decorator-design-pattern/ | 2024-03-29T11:10:30+00:00
https://github.com/addpipe/simple-recorderjs-demo