Using the Decorator Pattern to handle cross-cutting concerns
Hi All! Today I’ll be writing about a very simple but powerful technique to reduce boiler-plate caused by cross-cutting concerns. We’ll explore a simple way to encapsulate them in reusable components using the Decorator pattern.
I was actually planning of posting this article here but I was migrating to another server the last week and it took one week for the domain to point to the new DNS. Turns out this gave me the chance to try Medium instead, so published my first article there.
Let’s first talk a bit about “cross cutting concerns”. On Wikipedia we can find this definition:
Cross-cutting concerns are parts of a program that rely on or must affect many other parts of the system.
In a nutshell, they represent almost everything not completely tied to the domain of the application but that can affect in some way the behaviour of its components.
Examples can be:
– error handling
Instrumentation for instance can lead to a lot of boilerplate code which eventually will create clutter and pollute your codebase. You’ll basically end up with a lot of code like this:
Of course, being IT professionals, you can quickly come up with a decent solution, find the common denominator, extract the functionality, refactor and so on.
So…how would you do it? One option would be to use the Decorator pattern! It’s a very common pattern and quite easy to understand:
Basically you have a Foo class that you need somewhere that implements a well known interface, and you need to wrap it into some cross-cutting concern. All you have to do is:
- create a new container class implementing the same interface
- inject the “real” instance
- write your new logic where you need
- call the method on the inner instance
- sit back and enjoy!
Very handy. Of course it can be quite awkward in case your interface has a lot of methods, but in that case you might have to reconsider your architecture as it is probably breaking SRP.
One option would be moving to CQS / CQRS. In the next post of the series we will see a practical example and discuss why those patterns can be an even more interesting option when combined with Decorators.