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.

Creating chaos with inheritance

Creating chaos with inheritance

From the course: Programming Foundations: Design Patterns

Start my 1-month free trial

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…

Contents