The Strategy Pattern
According to the Gang of Four, the Strategy pattern’s intent is to
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.6
The Strategy pattern is based on a few principles:
- Objects have responsibilities.
- Different, specific implementations of these responsibilities are manifested through the use of polymorphism.
- There is a need to manage several different implementations of what is, conceptually, the same algorithm.
It is a good design practice to separate behaviors that occur in the problem domain from each other—that is, to decouple them. This allows me to change the class responsible for one behavior without adversely affecting another.
Figure 9-6 Generic structure of the Strategy pattern.