The strategy pattern, also known as the policy pattern, defines a series of algorithms, encapsulates each algorithm, and allows them to be interchangeable. The strategy pattern allows algorithms to vary independently from clients that use them. This type of design pattern belongs to the behavioral pattern category.
Completing a task can often be achieved in multiple ways, each way is known as a strategy. Depending on the different environment or conditions, we can choose different strategies to accomplish the task at hand. In software development, we often encounter similar situations where there are multiple approaches to implementing a certain functionality. In such cases, a design pattern can be used to make the system flexibly choose the approach for solving the problem, and also make it easy to add new approaches. In a software system, there may be many algorithms that can implement a certain functionality, such as searching or sorting. One common approach is hard coding these algorithms in a class. For instance, if multiple search algorithms need to be provided, these algorithms can be written in a class, with each method corresponding to a specific search algorithm. Another approach is to encapsulate these search algorithms in a unified method, using conditional statements such as if-else
to make the choice. Both of these approaches can be referred to as hard coding. If a new search algorithm needs to be added, the source code of the encapsulated algorithm class needs to be modified, and the client calling code also needs to be modified. The class encapsulating the algorithms will become complex and difficult to maintain. Besides providing a dedicated search algorithm class, the algorithm code can also be directly included in the client program, which is even less advisable as it would lead to a large and hard-to-maintain client program, especially when there are numerous available algorithms to choose from. To address these issues, independent classes can be defined to encapsulate different algorithms, with each class encapsulating a specific algorithm. Here, each class encapsulating the algorithm can be referred to as a strategy. To ensure the consistency of these strategies, an abstract strategy class is generally used to define algorithms, with each specific algorithm corresponding to a specific concrete strategy class. While the state pattern changes the State
objects combined by the Context
through state transitions, the strategy pattern changes the combined Strategy
objects through the Context
's own decision-making.