- How do you use strategy patterns?
- How is the strategy pattern defined?
- Why is it called strategy pattern?
- What problem does strategy pattern solve?
How do you use strategy patterns?
Use the Strategy pattern when you want to use different variants of an algorithm within an object and be able to switch from one algorithm to another during runtime. Use the Strategy when you have a lot of similar classes that only differ in the way they execute some behavior.
How is the strategy pattern defined?
In computer programming, the strategy pattern (also known as the policy pattern) is a behavioral software design pattern that enables selecting an algorithm at runtime. Instead of implementing a single algorithm directly, code receives run-time instructions as to which in a family of algorithms to use.
Why is it called strategy pattern?
The idea behind the strategy pattern is that "algorithms can be selected at runtime." (Wikipedia, Strategy Pattern) The pattern, in essence, is selecting the right strategy (or behavior) for solving a particular problem at runtime. Hence, it's name.
What problem does strategy pattern solve?
Strategy Pattern: Problems It Solves
Strategy Pattern prevents hard-wiring of all the algorithms into the program. This makes our program complex and much more bogus and hard to refactor/maintain and understand. This, in turn, makes our program to contain algorithms they do not use.