APPLIED DESIGN PATTERNS
This Is How You Extend Your Classes Without Modifying Them
Plain and simple extension over modification using decorators
Ever had new requirements thrown at you where you’d need to add new functionality to a class — even if it’s probably not the class’ main responsibility?
Say you’re told to add some log traces in a repository class. Easy, receive a logger object in the constructor and slap those log statements in there. A week later you need to add caching functionality, now, you’re starting to bloat the constructor and method complexity rises.
It’s quite possible that it’ll go on like this.
You want a way out of this increasing maintainability nightmare. Preferably a simple, easy way out.
In this article we’re looking into the following
👉 Extract an interface to apply the decorator pattern
👉 Extend a class’ functionality without modifying it
👉 Register the decorated class with a Dependency Container