课程: Programming Foundations: Design Patterns

今天就学习课程吧!

今天就开通帐号,24,100 门业界名师课程任您挑!

Understanding the decorator pattern

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,…

内容