The Decorator Pattern

According to Gang of Four, the Decorator pattern's intent is to

Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.[1]

[1] Gamma, E., Helm, R., Johnson, R., Vlissides, J., Design Patterns: Elements of Reusable Object-Oriented Software, Reading, Mass.: Addison-Wesley, 1995, p. 315.

The Decorator pattern works by allowing me to create a chain of objects that starts with the decorator objects—the objects responsible for the new function—and ends with the original object. Figure 15-3 illustrates this.

Figure 15-3. The Decorator chain.

The class diagram of the Decorator pattern in Figure 15-4 implies the chain of objects shown in Figure ...

Get Design Patterns Explained: A New Perspective on Object-Oriented Design now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.