Decorator Pattern - The Decorator Model (Page 3 of 4 )
The Decorator outlined in the class diagram notation shows two key elements: component and decorator. The component represents whatís to be decorated, and the decorator is the abstract class for the concrete decorations. The concrete component is whatís actually decorated, and the concrete decorations are the actual decorations. All the concrete decorations and the concrete component are subclassed from the same source. Keeping that in mind, take a look at the class diagram in Figure4-1.
Figure 4-1. Decorator design pattern
In the most basic sense, the component is the Christmas tree, and the decorations are the ornaments. Each concrete decorator wraps the tree and is subclassed from the same source as the tree. A better way to think about components and decorators is more like the nesting dolls from Japan and Russia. The innermost doll is the component, and itís placed into a decorator. Then the component and decorator are placed into another decorator, and then into the next decorator. Figure 4-2 shows this model.
Figure 4-2. Component wrapped in series of decorators
Probably the most important thing to remember about the Decorator pattern is that its purpose is to allow additional elements to be added to a class without altering the base class. So instead of making changes by changing the class, the class is decorated with subclasses that donít interfere with the class from which the decorators were derived.