From the course: Programming Foundations: Design Patterns

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Solution: The Decorator pattern

Solution: The Decorator pattern

From the course: Programming Foundations: Design Patterns

Start my 1-month free trial

Solution: The Decorator pattern

(funky upbeat music) - Here's our solution for creating pizzas with the decorator pattern. In this solution, we've closely followed the decorator pattern structure. First, we define a Pizza class that has a getDescription method and an abstract cost method. Then we added ThinCrustPizza and ThickCrustPizza concrete subclasses, which both extend the Pizza class. We also created a ToppingDecorator class that, again, following the structure of the decorator pattern, extends the Pizza abstract class. Note that ToppingDecorator has a property Pizza and an abstract method getDescription. Extending the ToppingDecorator, we've created classes Cheese, Olives, and Peppers for our toppings. We'll pass in the pizza the toppings are decorating and the constructor, and store the pizza in the Pizza variable that's inherited from the ToppingDecorator class. The cost method in the pizza classes return the cost of the basic thin or thick crust pizza, while the cost method in the toppings classes returns…

Contents