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.

Understanding the decorator pattern

Understanding the decorator pattern

From the course: Programming Foundations: Design Patterns

Start my 1-month free trial

Understanding the decorator pattern

- [Narrator] Now that you're familiar with the decorator pattern, let's take a look at the definition from the Gang of Four catalog. The decorator pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality, and we have a sense of how this design pattern can help us make a flexible design from our coffee example. As you're seeing, the formal definition of a pattern usually describes the role of the pattern, but it doesn't necessarily help us know how to implement it in our own designs. As with all design pattern definitions, we really need to look at a class diagram to get a better sense for how the pattern works. There are two important parts to the decorator pattern. There are the components which are the beverages in our example and the decorators which are the condiments in our example. Starting at the top of this diagram, you can see that we have a component class. This is an…

Contents