11.1 The Decorator pattern
The Decorator pattern allows us to wrap an object that provides core functionality with other objects that alter this functionality. Any object that uses the decorated object will interact with it in exactly the same way as if it were undecorated (that is, the interface of the decorated object is identical to that of the core object).
The Decorator design pattern is part of the conceptual background behind the Python language decorator, used to add features to functions and classes.
There are two primary uses of the Decorator pattern:
-
Enhancing the response of a component as it sends data to a second component
-
Supporting multiple optional behaviors
The second option is often an alternative to multiple inheritance, focused on composition. We can construct a core object, and then create a decorator wrapping that...