课程: Programming Foundations: Design Patterns

今天就学习课程吧!

今天就开通帐号,24,100 门业界名师课程任您挑!

Using the Decorator pattern

Using the Decorator pattern

- [Female Instructor] Here's how we implement Starbuzz Coffee using the decorator pattern. Remember, our new design uses a common super class beverage for both the coffees and the decorators. And each decorator is composed with the beverage it is wrapping. We'll start at the top with the beverage class. This is our component super class. It implements a get description method that returns the string unknown beverage. But hopefully the concrete component classes will override this to a better description. And it specifies an abstract method cost that subclasses must implement. Now let's take a look at one of the concrete components. These are the coffees that extend the beverage class. For instance, the dark roast class extends beverage and sets the description string it inherits from beverage to dark roast coffee, so we get a good description of the coffee. Then, dark roast implements the cost method and simply returns 99…

内容