2007 |
Section 9, Chapter 3 |
Strategy Pattern
Concept
A Strategy pattern is a group of algorithms encapsulated inside classes that are made interchangeable so the algorithm can vary depending on the class used.
Use
Strategies are useful when you would like to decide at run time when to implement each algorithm.
Design
The Strategy pattern has three main components:
- the Strategy: a common interface that ties together all supported algorithms. Usually this class is abstract;
- the Concrete Strategy: the implementation class where the logic for each different algorithm lives in the group;
- the Context: the object that ties a concrete strategy to a particular code flow.