Decorator Design Pattern

Himanshu verma
3 min readApr 21, 2023

--

What is Decorator Design Pattern ?

Decorator pattern allows a user to add new functionality to an existing object without altering its structure. This type of design pattern comes under structural pattern as this pattern acts as a wrapper to existing class.

Same question comes in mind after reading the above statement “WHAT?”

Let me explain it in layman terms ;)

Let’s say we have an object, and it has property “a”, now we cover it with another object which had property “b”,so now the property of the outermost object would be “a” and “b” both.

If we cover the second object with another object which has property “c” so now properties would be “a”, “b” and “c”.So it is kind of loop (one object in another and another object in some another) *NESTED LOOP*

Let me explain it more clearly via Example

For easy understanding taking an example of Burger

Let’s say we have burger with Potato Patties only .We have different options(property’s) to add to this burger itself :-

- With Veggies

- With Cottage cheese

- With Butter/ Without Butter

- With Cheese

So, we had burger with Potato Patties, now we added another property into it, i.e veggies, so our burger now become potato patties plus veggies burger

Now we add Cottage cheese to it, so our burger now became potato patties plus veggies plus Cottage cheese burger

So we are adding Decorator on an object (Potato Patties Burger), decorators are :- veggies , cottage cheese, which themselves are objects.

PROBLEM

If we have base (Potato patties) and we add decorators to it, but there would be several permutations and combinations to it, first we would add veggies or first we would add Cottage cheese and many more combinations too, so this will lead to “CLASS EXPLOSION”.

Lets jump on to code now, to understand it better


abstract class baseBurger {
abstract fun cost(): Int
}

class VeggieBurger : baseBurger() {
override fun cost(): Int {
return 200
}
}

class CottageCheeseBurger : baseBurger() {
override fun cost(): Int {
return 300
}

}


abstract class ToppingDecorator : baseBurger() {
}

class ExtraCheese(burger: baseBurger) : ToppingDecorator() {
/**
* Constructor injection (baseBurger)
*/
var burgerType: baseBurger = burger
override fun cost(): Int {
return burgerType.cost() + 40
}
}
class ExtraButter(burger: baseBurger):ToppingDecorator()
{
/**
* Constructor injection (baseBurger)
*/
var burgerType: baseBurger = burger
override fun cost(): Int {
return burgerType.cost() + 20
}

}
class Main{
val vegBurgerWithExtraCheese = (ExtraCheese(VeggieBurger()))
val cottageCheeseBurgerWithExtraButter = (ExtraButter(CottageCheeseBurger()))
val cottageCheeseBurgerWithExtraButterWithExtraCheese = ExtraCheese(ExtraButter(CottageCheeseBurger()))
}

There are two types of burger having different cost. Now we need to add toppings on to the burger, so we used constructor injections in toppings classes (ExtraCheese, ExtraButter) by doing this we passed type of burger directly in it .

How Decorator Pattern helped?

As we can see now we can add as many “ExtraChees” / “ExtraButter” to our VeggieBurger/ CottageCheeseBurger easily without any Class Explosion.

Hope you got the idea of Decorator Design Pattern :)

Links to entire series of Design Pattern

Starting with Solid principles :- https://medium.com/@himv1998/s-o-l-i-d-principle-a06df556a127

Strategy Design Pattern :- https://medium.com/@himv1998/strategy-design-pattern-73b0ddd7495c

Observer Design Pattern :- https://medium.com/@himv1998/observer-design-pattern-e5b601211b3

References :- https://www.tutorialspoint.com/design_pattern/decorator_pattern.htm

In case of doubt , ASK IT OUT :-https://www.linkedin.com/in/himanshu-verma-318903171/

--

--