Pattern 9 | Replacing Decorator |
Intent
To add behavior to an individual object rather than to an entire class of objects—this allows us to change the behavior of an existing class.
Overview
Decorator is useful when we’ve got an existing class that we need to add some behavior to but we can’t change the existing class. We may want to introduce a breaking change, but we can’t change every other part of the system where the class is used. Or the class may be part of a library that we can’t, or don’t want to, modify.
Decorator uses a combination of inheritance and composition. It starts with an interface with at least one concrete implementation. This implementation is the class that we can’t or don’t want to change.
We then implement the interface ...
Get Functional Programming Patterns in Scala and Clojure now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.