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 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
- The Decorator Pattern | Set 2 (Introduction and Design)
- Decorator Pattern | Set 1 (Background)
- MVC Design Pattern
- Builder Design Pattern
- Visitor design pattern
- Composite Design Pattern in C++
- Composite Design Pattern
- Proxy Design Pattern
- Mediator Design Pattern
- Flyweight Design Pattern
- Mediator design pattern
- Bridge Design Pattern
- Prototype Design Pattern
- Memento design pattern
- Interpreter Design Pattern
- State Design Pattern
- Singleton Design Pattern | Implementation
- Front Controller Design Pattern
- Facade Design Pattern | Introduction
- Object Pool Design Pattern