From the course: Programming Foundations: Design Patterns
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Using the Decorator pattern
From the course: Programming Foundations: Design Patterns
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…
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)
-
-
-
-