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 15-3. Each chain starts with a Component (a ConcreteComponent or a Decorator). Each Decorator is followed either by another Decorator or by the original ConcreteComponent. A ConcreteComponent always ends the chain.

Figure 15-4. The Decorator pattern class diagram.


For example, in Figure 15-4, ConcreteDecoratorB performs its Operation and then calls the Operation method in Decorator. This calls ConcreteDecoratorB's trailing Component's Operation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.142.128