One of my favorite patterns is a decorator. I love it because we store implementation in different classes and then combine them according to business specification. Our classes become small, testable and easy to understand. Moreover, we group our logic in one layer.
I think ‘decorator’ is truly one pattern that can achieve SRP
Let’s get down to the practice. Now I`m trying kotlin and all code will be written in this awesome language ;)
Here are our simple requirements: when the user clicks the button we should show the dialog with the phrase. This phrase can consist of multiple random words, joined by space.
We would write just layer responsible for ‘Phrase’
Let`s write an implementation of our business requirements.
Joined by space:
After implementation we can continue with our configuration layout.
If you run this code in the console (or REPL) you will see probably the similar output.
When you get new requirements, you need just to change configuration layer. If the project is new, we introduce more and more decorators. But after a while, we start to reuse them without writing the new one.
Decorator pattern is very powerful. The main difference with the standard iffor approach is code reuse. It is easy to reuse class then method. Also, it is much easier to test them. One drawback is unusual code style. But it becomes more familiar after a while. If you don’t use decorators just try it. You will definitely love it.