Decorator Design Pattern
Get an overview of the decorator design pattern.
We'll cover the following...
Overview
Suppose that we’re in a restaurant and order some beverages from a list (coke, coffee, tea). But along with the different drinks, the restaurant also gives us the option of adding ice and lemon to our beverage (with an additional cost).
One way to program this problem would be to create a base class of beverages and inherit multiple classes like Coke
, Coke_lemon
, Coke_ice
, Coffee
, Cold_coffee
, and so on. Just by looking at the names of the classes, we can see that there will be a problem if the restaurant wants to add more beverages or add-ons. This will exponentially increase the number of classes.
One way to approach this problem would be to use the bridge pattern, as discussed previously. But there’s a problem: what if we want double ice or lemon in our beverage, or lemon in addition to ice? This is where the bridge pattern fails to ...