Exploring Design Patterns — Decorator Pattern (The Dosa billing)

Koushikr
3 min readMar 29, 2022

Hey Guys,

If you are reading this article, then I assume that you are looking for answers to questions like what is a decorator pattern? Why should we use it? When should we use it? And etc., let’s answer all these questions by exploring them.

Picture of Dosa

So, let us start by considering we have class Dosa (Dosa is South Indian dish, but why dosa? not pizza? — the answer is simple because I like Dosa more than Pizza). The Dosa class has a property “price” and getter method “getPrice”, and it looks like something like the code snippet below.

This is all simple whenever a Dosa is ordered, a new Dosa object is created, and during billing “getPrice” method is called, and the value “100” is billed.
There is no problem here. Being an owner of a restaurant, would the owner serve only ordinary Dosa to customers? Wouldn’t the owner want to attract customers and grow the business? But, how would the owner do this? I can hear your mind voice — yes, you are right. To grow the business, the owner would add extra features to the ordinary Dosa and attract customers.

Initially, the owner starts with a feature — type of batter. The types of batter are Rice, Wheat and Rava. Now, would the owner be selling all these types of Dosa for the same price? The simple answer is “No”. As a programmer, we need to handle all these types of Dosa in our code. What do we do now? — A simple answer is “inheritance”. We would extend the base class Dosa and create three new classes for Rice, Wheat and Rava. These classes look something like the code snippet below.

Now, everything is good. But, owner wants to improve his service. The restaurant owner adds one more feature to Dosa to grow the business. Now the feature is roasting type. The types are Oil, Ghee and Butter. The options available on the menu are Dosa, Rice Dosa, Wheat Dosa, Rava Dosa, Rice Oil Dosa, Rice Ghee Dosa, Rice Butter Dosa, Wheat Oil Dosa, Wheat Ghee Dosa, Wheat Butter Dosa, Rava Oil Dosa, Rava Ghee Dosa and Rava Butter Dosa. As a programmer, we have got a lot of work to do following the “inheritance” solution. Create a class for each type of Dosa in the menu, 13 classes. Oh! It is a lot of classes, right?

The owner wouldn’t stop here. He would try to add more features to Dosa, like topping. The types of topping are Masala, Cauliflower, Mushroom, Chilly and Onion. Now, the number of classes is 66. Would we still think of the “inheritance” solution? Yes, you are right — the answer is “No”. Because the number of classes can increase exponentially with features.

So, what is the solution? Design patterns can help us here-the “DECORATOR PATTERN”. Finally, we will address the decorator pattern. Decorator pattern can solve this problem. But how? We shall have a look. Inheritance solution uses “is-a” relationship while decorator pattern uses “has-a” relationship. Still not clear? The code snippet below can help you.

The above snippet show adding features to Dosa layer by layer. Adding Batter layer over ordinary layer, then adding Roast layer over Batter layer and then adding Topping layer over Roast layer. The same pattern can be changed without layering based on requirement.

We have reduced the number of classes of complexity m * n * o + 1 to m + n + o + 4. Where m is the number of first layer features, n is the number of second layer features, and o is the number of third layer features.

Hope this help!

Reference:

  1. Elisabeth Freeman, Eric Freeman, Bert Bates, and Kathy Sierra. 2004. Head First Design Patterns. O’ Reilly & Associates, Inc.

--

--