This is the third chapter of the “Head First Design Patterns” book. I have covered the Strategy and Observer patterns before.
The problem
We are told to design an ordering application at a coffee shop. It sells a variety of beverages like
- House Blend
- Dark Roast
- Decaf
- Espresso
Apart from these, the customer has the choice to have condiments (add-ons) like
- Steamed milk
- Soy
- Mocha
- Whipped milk
Press enter or click to view image in full size![a collage of coffee and various condiments]()
Thought process
Approach 1
- What a noob coder like me would do is make an abstraction of
Beverage, and derive concrete implementations of House Blend, Dark Roast, and so forth. - The problem arises with the condiments, using the above approach we are spoiled with choices and our implementations become
House Blend with steamed milk and mocha
Dark Roast with whip and soy
- Just imagine the number of…
