From the course: Programming Foundations: Design Patterns
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Solution: The Decorator pattern
From the course: Programming Foundations: Design Patterns
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
(Locked)
Creating chaos with inheritance4m 12s
-
(Locked)
Understanding the open-closed principle2m 49s
-
(Locked)
Extending behavior with composition2m 29s
-
(Locked)
Understanding the decorator pattern2m 23s
-
(Locked)
Using the Decorator pattern5m 6s
-
(Locked)
Challenge: The Decorator pattern40s
-
(Locked)
Solution: The Decorator pattern1m 22s
-
(Locked)
-
-
-
-