The Strategy Pattern
This is the first post in a series of blogposts on Design Patterns. The strategy pattern is used when various implementations of a single behavior exists and we want to decouple the consumers of this behavior from any particular implementation.
Intent
- Make a different behavior at different times for different clients requesting the service.
- Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
Example
A Strategy defines a set of algorithms that can be used interchangeably. Some examples where this can be applied -
-
Modes of transportation to an airport is an example of a Strategy. Several options exist such as driving one’s own car, taking a taxi, an airport shuttle, a city bus, or a limousine service. All the modes perform the same function.
-
Various compression or decompression algorithms is another example of a Strategy. Depending on certain factors, the Context can change the compression algorithm which will perform both the compress and decompress operations.
Structure
In this example, the Client calls the Context object. The Context object, gets an instance of the Strategy (either ConcreteStrategy1 or ConcreteStrategy2 or another ConcreteStrategy) object depending on some variable. If there is a new strategy, then just these 2 simple steps need to be done -
- Add a new strategy object extending Strategy.
- Update the logic in the Context to decide when this new ConcreteStrategy would be picked.
One way to achieve this behavior through simple code is by a simple if-else statement (or any other branching logic) -
The disadvantage of this approach is that -
- If the logic that decides the algorithm is needed at multiple places, then the code has to be repeated.
- More codebase reading for a new developer trying to add a new algorithm.
Code
A sample code structure in Java -
Kaushik Rangadurai
Code. Learn. Explore