| 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access