课程: Programming Foundations: Design Patterns
今天就学习课程吧!
今天就开通帐号,24,100 门业界名师课程任您挑!
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…
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。
内容
-
-
-
-
-
-
-
(已锁定)
Creating chaos with inheritance4 分钟 12 秒
-
(已锁定)
Understanding the open-closed principle2 分钟 49 秒
-
(已锁定)
Extending behavior with composition2 分钟 29 秒
-
(已锁定)
Understanding the decorator pattern2 分钟 23 秒
-
(已锁定)
Using the Decorator pattern5 分钟 6 秒
-
(已锁定)
Challenge: The Decorator pattern40 秒
-
(已锁定)
Solution: The Decorator pattern1 分钟 22 秒
-
(已锁定)
-
-
-