Decorator Pattern | Set 3 (Coding the Design)
We have discussed Pizza design problem and different naive approaches to solve it in set 1. We have also introduced Decorator pattern in Set 2. In this article, design and implementation of decorator pattern for Pizza problem is discussed. It is highly recommended that you try it yourself first. The new class diagram (Click on the picture to see it clearly)
- Pizza acts as our abstract component class.
- There are four concrete components namely PeppyPaneer , FarmHouse, Margherita, ChickenFiesta.
- ToppingsDecoratoris our abstract decorator and FreshTomato , Paneer, Jalapeno, Barbeque are concrete decorators.
Below is the java implementation of above design.
Margherita Cost :100 FarmHouse, Fresh Tomato , Paneer Cost :310
Notice how we can add/remove new pizzas and toppings with no alteration in previously tested code and toppings and pizzas are decoupled.
Basic Milk shake MilkShake Price of MilkShake : 30 Mango decorated Milk shake MilkShake decorated with Mango Price of Mango MilkShake : 70 Vanilla decorated Milk shake MilkShake decorated with Vanilla Price of Vanilla MilkShake : 110
Further Read: Decorator Method in Python
This article is contributed by Sulabh Kumar and Shashank Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Please Login to comment...