Card 08/ 08
Recap: Decorator Pattern
Same interface, one wrapper per feature, and the order of wrapping matters.
Decorator adds behaviour to one object at run time by wrapping it in objects that share its interface.
- Intent. Attach additional responsibilities to an object dynamically, as a flexible alternative to subclassing.
- Participants. The component interface, the concrete component, and decorators that both implement and hold a component.
- Recognise it by shape. A class that implements an interface and holds an object of the same interface, adding work around each call.
- Order. Calls travel inwards and results outwards, so the innermost decorator acts first on the result, and swapping two layers can change the outcome.
- In the wild.
java.io:BufferedInputStreamwraps anyInputStreamthroughFilterInputStream. - Python's
@. It is related but different, because it wraps a function once at definition time. Usefunctools.wrapsto keep the name.
← → move through sections and cards