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
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 firstname.lastname@example.org. 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
- Design Patterns | Set 1 (Introduction)
- Design Patterns | Set 2 (Factory Method)
- Command Pattern
- Observer Pattern | Set 1 (Introduction)
- Observer Pattern | Set 2 (Implementation)
- Singleton Design Pattern | Implementation
- Decorator Pattern | Set 1 (Background)
- The Decorator Pattern | Set 2 (Introduction and Design)
- Strategy Pattern | Set 1 (Introduction)
- Strategy Pattern | Set 2 (Implementation)
- Adapter Pattern
- Iterator Pattern
- Curiously recurring template pattern (CRTP)
- Flyweight Design Pattern
- Implementing Iterator pattern of a single Linked List