From the course: Programming Foundations: Design Patterns
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Creating chaos with inheritance
From the course: Programming Foundations: Design Patterns
Creating chaos with inheritance
- We're now going to explore a pattern called the decorator pattern, and to get a better understanding for this pattern we're going to start with a small example. Let's say you want to build an order system for a small coffee shop, one which serves and takes payments for beverages. The shop has four main types of beverages on the menu, each of which has a description and a cost. And to each beverage you can add a number of condiments like soy or milk or whip or mocha. Each of these condiments has a small cost which needs to be added to the cost of the coffee. Let's sketch out a design for the coffee shop's order system. We'll start with a beverage superclass which has a description field and a corresponding getter method as well as a cost method. Now let's add some subclasses like house blend, dark roast, decaf, and espresso. Note that each coffee is a beverage. In other words, we have an is-a relationship between the…
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)
-
-
-
-