Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
- defines the interface for objects that can have responsibilities added to them dynamically.
- defines an object to which additional responsibilities can be attached.
- maintains a reference to a Component object and defines an interface that conforms to Component's interface.
- adds responsibilities to the component.
- More flexibility than static inheritance.
- Avoids feature-laedn classes high up in the hierarchy.
- Simplifies coding because you write a series of classes each targeted at a specific part of the functionality rather than coding all behavior into the object.
- Enhances the object's extensibility because you make changes by coding new classes.
- When you want to add responsibilities to individual objects dinamically and transparently, without affecting other objects.
- When you want to add responsibilities to the object that you might want to change in the future.
- When extension by static subclassing is impractical.