Decorator
Intent
Attach additional responsibilities to an object dynamically.
Decorators provide a flexible alternative to subclassing for extending
functionality.
Participants
-
Component -
defines the interface for objects that have responsibility added to
them dynamically.
-
Concrete Component -
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.
-
Concrete Decorator -
adds responsibilities to a Component.
Notes
-
Also known as Wrapper.
-
An alternative to Stategy for avoiding the duplication of class
hierarchies and inflexibility that results from using inheritance to
add functionality.
Examples
-
JScrollPane
is a Decorator for Swing components.
-
Border
is a Strategy for Swing components.