The Strategy Design Pattern
The Strategy pattern defines a family of algorithms and makes them interchangeable.
Use the Strategy pattern when you want to define a family of algorithms, encapsulate each one of them, and make them interchangeable. Also useful when you want to avoid exposing the complex, algorithm-specific data structures.
- TypeBehavioral
- Time ComplexityO(1) Constant Time – The algorithm runs in constant time, irrespective of the input size. Operations like accessing an array element fall into this category.
- Efficiency8
-
Learning Effort
- Average5
- Conceptual5
- Debug and Maintain6
- Implementation3
- Prerequisites4
- Versatility7
Things you should know before you start
- Encapsulation Understanding of encapsulation is needed as the Strategy pattern encapsulates algorithms into separate classes.
Minimal Example
Implementing different sorting algorithms for a list of numbers, allowing the user to choose which one to use.
interface SortingStrategy { sort(array: number[]): number[]; } const sorter = new Sorter(new QuickSort()); const sortedArray = sorter.sort([5, 2, 9, 1]);
What’s happening in thie example
In this example, the `SortingStrategy` serves as the Strategy interface, and `QuickSort` is a ConcreteStrategy. The `clientCode` shows how to use the Strategy pattern to sort an array using a specific algorithm.
How the Strategy Works
The Strategy pattern is particularly useful when you have multiple ways of performing a specific operation and want to make the algorithm easily switchable.
It allows the algorithm to vary independently from the context that uses the algorithm.
Entities in the Strategy Design Pattern
-
StrategyThe Strategy interface declares operations common to all supported algorithms.
- Strategy has a one-to-many implements relationship with ConcreteStrategy
What to watch out for when using or considering this design pattern.
Avoid using the Strategy pattern when the algorithm variations are very few and unlikely to change in the future, as it could introduce unnecessary complexity.
Design patterns often confused with the Strategy Design Pattern
The following design patterns are often confused with the Strategy
for various resons.
If you’re exploring solutions, have a look through this list and see if
one of these might be a better fit for your problem.
-
State – The State pattern allows an object to alter its behavior when its internal state changes.
The State pattern is similar to the Strategy in the following ways.
- Both patterns aim to encapsulate varying behavior in separate classes.
- Both are behavioral design patterns.
- Strategy is designed for switching algorithms, while State is for switching object behaviors based on internal states.
-
Template Method – The Template Method pattern defines the skeleton of an algorithm in the superclass but lets subclasses override specific steps of the algorithm without changing its structure.
The Template Method pattern is similar to the Strategy in the following ways.
- Both patterns aim to encapsulate algorithmic details.
- Both can be used to implement a family of algorithms.
- Strategy uses composition to switch between algorithms, while Template Method uses inheritance.