The Decorator design pattern provides a solution:
Define separate Decorator
objects that add responsibilities to an object.
Work through Decorator objects
to extend the functionality of an object at run-time.
Describing the Decorator design in more detail is the
theme of the following sections.
See Applicability section for all problems Decorator can solve.
Decorator objects
that 'decorate' (add responsibilities to)
an (already existing) object.
Component interface transparently
so that it can act as transparent enclosure
of the component that gets decorated.
Decorator
objects:
Decorator)
that maintains a reference
to a Component object (component)
and forwards requests to this component
(component.operation()).
Decorator1,…)
that implement additional functionality
(addBehavior())
to be performed before and/or after
forwarding a request.
Component1
object (after forwarding the request).