Decorator Pattern
Overview
Attach additional responsibilities to an object dynamically. Decorators provide
a flexible alternative to subclassing for extending functionality.
Participants
- defines the interface for objects that can have responsibilities added
to them dynamically.
- this participant can not be read-only.
- defines an object to which additional responsibilities can be attached.
- this participant can not be read-only.
- maintains a reference to a Component object and defines an interface that
conforms to Component's interface.
- this participant can not be read-only.
- this participant can not be an interface.
- adds responsibilities to the component.
- this participant can not be read-only.
- this participant can not be an interface.
Parameters
-
here you can specify the name of a Decorator's attribute where it keeps a reference
to a Component object.
-
the "Initialization variant" option defines the kind of initialization of that attribute.
-
when selected, will create special JavaDoc links in the code between pattern participants, describing
their relationships.
Applicability
Use Decorator
- to add responsibilities to individual objects dynamically and transparently, that is, without
affecting other objects.
- for responsibilities that can be withdrawn.
- when extension by subclassing is impractical. Sometimes a large number of independent extensions
are possible and would produce an explosion of subclasses to support every combination. Or a class
definition may be hidden or otherwise unavailable for subclassing.