A GUIDE TO SOFTWARE DESIGN PATTERNS
Behavioral Design Pattern: Strategy
The strategy pattern enables algorithms to switch at runtime. In plain English, that simply means deciding at runtime which concrete implementation of an interface to use using a context, which is where the actual concrete implementation injection happens. Deferring the decision on which algorithm to use until runtime allows more flexibility, reusability. It also makes the codebase more maintainable
This pattern comes in handy when multiple algorithms for a given strategy (interface) is needed. including the client, implementation of this pattern requires four components:
- Client -> where the context gets used
- Context -> where an algorithm is selected at runtime
- Strategy -> interface
- Algorithms -> concrete implementation
I’ve always found it easier to grasp new concepts using real-world scenarios. Let’s use one to see how we can how we implement it using this pattern.
Let’s say you’re part of a team that’s creating an application that does something cool. One of the must-have…