Deck the halls — The Design Pattern

Sameeha Rahman
5 min readJul 13, 2019

I’m gonna give you a small scenario.

Say you own a coffee shop, and like any newbie, start out with just two types of plain coffee, the house blend, and dark roast. In your billing system, there was one class for the different coffee blends, which inherits the beverage abstract class. People actually start to come by and have your wonderful (albeit bitter😖) coffee. Then there are the coffee newbs that, God forbid, want sugar or milk. Such travesty to coffee!! 😱😱

Now you need to have those two add-ons as well, both to the menu, and unfortunately on the billing system. Originally, your IT guy will make a subclass for both coffees, one including sugar, the other milk. Then, since customers are always right, one says these dreaded words;

“Can I get a milk coffee, with sugar, please?”

😒😒😒

There goes your billing system laughing in your face again. Well, back to the drawing board….

The IT guy then adds milk coffee with sugar as another subclass to each parent coffee class. The rest of the month is smooth sailing, people lining up to have your coffee, you actually making money. 💸💰

But wait, there’s more!

The world is against you once again. A competitor opens up across the street, with not just 4 types of coffee, but more than 10 add-ons as well! 😤

You buy all those and more, to sell better coffee yourself, and just then remember that you forgot to update that dratted billing system. You quite possibly cannot make the infinite number of subclasses for any and all combinations of all the add-ons, with the new coffee blends too. Not to mention, the size of the final system.😩😩

Time to actually invest in a proper billing system. You find a new IT personnel, who actually knows what they are doing and they say;

“Why, this will be so much easier and smaller, if it used the decorator pattern.”

What on earth is that?

Well, its what this post is all about.

Decorator design pattern falls into the structural category, that deals with the actual structure of a class, whether is by inheritance, composition or both. The goal of…

--

--

Sameeha Rahman

Front End Developer at Switchd Ltd. | MSc in Computer Science @ QMUL (2022)