From the course: Programming Foundations: Design Patterns

Unlock the full course today

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

Using the Decorator pattern

Using the Decorator pattern

From the course: Programming Foundations: Design Patterns

Start my 1-month free trial

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 cents, which is the cost of a basic dark roast coffee with no…

Contents