Strategy Design Pattern
1 min readJan 22, 2023
The strategy pattern is a behavioral design pattern that enables selecting an algorithm at runtime — Wikipedia.
The strategy design pattern is a behavioral design pattern that allows an object to change its behavior at runtime by providing its internal algorithms (like Octal, Binary, Hexa in UML design).
Advantage:
- Algorithm independency: An object of Strategy Design allows to change the strategy algorithm in the run time.
- Extensibility: Able to add new algorithm in system without changing the existing code.
- Loosely couple: The strategy pattern promotes loose coupling between the objects and algorithm.
Real Example:
- “Collections.sort(List, comparing_algorithm)” is used the strategy design pattern. Comparing algorithm could be ascending, descending or any custom sorting format.
- Design between Controller and View of MVC framework is Strategy Design pattern.
When to use:
Strategy pattern is used when we have multiple algorithms for a specific task, and the client decides the actual implementation be used at runtime. We define multiple algorithms and let client applications pass the algorithm to be used as a parameter. — Digital Ocean.