Decorator pattern is used to add additional responsibility/ functionality to existing behaviour of an object without altering its original structure.
This pattern creates a decorator class which is wrapper to the existing class and provides additional functionality keeping class methods signature intact.
The decorator pattern uses composition providing a flexible alternative to subclassing. Subclassing adds behaviour at compile time, and the change affects all instances of the original class, however decorator provides additional behaviour at runtime for individual objects.
What we get as a result of this:
“Concrete implementations is decoupled from dynamic behaviours and responsibilities.”
UML Class Diagram :
Thanks for reading this article. You may find the article on my Mediumblog as well.