THE STATE PATTERN
Arguably one of the simplest design patterns out there. A great place to get started.
How is this pattern going to help me?
- It helps to encapsulate a related set of algorithms. A family of calculations or assessments.
- Improve Separation of Concerns Principles and Open/Closed Principles.
Strategy Pattern Smells?
Smells! A smell in code is a bit of code you think smells real bad.
I have various ways of calculating the price of a product, they vary depending on the selected shipping method. I keep adding new ones and it’s getting crazy! Hmm…
When it comes to the Strategy pattern the following smells might be solvable…
- Lists of conditions or growing switch statements.
- Adding another similar ‘calculate cost’ method with a small variation. Another family member – an ugly uncle who drags the family down with all their boring nostalgia about transport and stream trains. meh.
The strategy allows us to encapsulate each algorithm into a separate concrete implementation that can easily be expanded upon.
Show Interfaces some love, and they will love yo back.