Applied Design Patterns with Java
Structural :: Decorator (175) {C ch 12}
Applicability
Use the Decorator pattern when:
- to add responsibilities to individual objects dynamically
and transparently without affecting other objects;
- for responsibilities that can be withdrawn;
- when extension by subclassing is impractical. Often,
a large number of independent extensions are possible and would produce an explosion of subclasses to for every
combination. Or a class definition may be hidden or otherwise unavailable for subclassing.
Structure
The Decorator pattern has this general organization:
Participants
- Component (VisualComponent) defines the interface for objects that can have responsibilities
added to them dynamically.
- ConcreteComponent (TextView) defines an object to which additional responsibilities can be attached.
- Decorator
maintains a reference to a Component object and
defines an interface that conforms to Component's interface.
- ConcreteDecorator (BorderDecorator, ScrollDecorator) adds responsibilities to the component.
Catalog Structural Prev Next