Design pattern Strategy in nutshell
The behavioral strategy help us to decouple if/else code.
- Definition
The definition in nutshell by GOF[2] is Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
2. How to use
This pattern is drawn in UML for generic and example
2.1 GOF
2.2. Example
Code
Conclusion
After make some tests and modeling this small example, this pattern helps to avoid a lot of if/else decisions and make the code more clear to read, decouple de algorithm as the PrintStrategy shows.
The complete code is available on my github.
References