Implementing the Decorator design pattern
The Decorator pattern allows us to extend objects at runtime while keeping responsibilities separated. It is a simple but powerful pattern. In this section, we'll explore how to implement this pattern in the traditional way, as well as how to leverage an open source tool named Scrutor to help us create powerful DI-ready decorators using .NET 5.
Goal
The decorator's goal is to extend an existing object, at runtime, without changing its code. Moreover, the decorated object should not be aware that it is being decorated, leaving it as a great candidate for long-lived or complex systems that need to evolve. This pattern fits systems of all sizes.
I often use this pattern to add flexibility and create adaptability to a program for next to no cost. In addition, small classes are easier to test, so the Decorator pattern adds ease of testability into the mix, making it worth mastering.
The Decorator pattern makes it easier to...