This article introduces the strategy pattern from the following aspects
The concept of strategy mode
An example of a strategy pattern
The advantages and disadvantages of the strategy mode
4. Summary
The concept of strategy mode
The strategy mode is the packaging of the algorithm, which separates the responsibility of using the algorithm from the algorithm itself, and assigns it to different objects for management. The strategy pattern usually wraps a series of algorithms into a series of strategy classes as a subclass of an abstract strategy class.
This mode involves three scenarios:
● Environment (Context) (for the client implementation): holds a reference to a Strategy.
● Abstract Strategy (Strategy) (behavior family): This is an abstract role, usually implemented by an interface or abstract class. This role gives all the interfaces required by concrete policy classes.
● ConcreteStrategy (ConcreteStrategy) (a subclass that implements the behavior family): Wraps related algorithms or behaviors.
Second, the specific example of the strategy pattern
The above text is excerpted from other blogs. It is difficult to understand. Use an example to deepen your understanding of the strategy pattern.
1. Algorithm Cluster
(1) For each strategy algorithm, they have common behaviors, so these common behaviors are abstracted into an interface, and then subclasses implement these interfaces to form an algorithm family
package net.oschina.design.strategy.interfac; /** * Strategy mode, sample sorting algorithm interface * * @author Freedom * */ public interface Sort { void sort(int[] array); }
(2) Subclasses implement specific behaviors
package net.oschina.design.strategy.interfac.impl; import java.util.Arrays; import net.oschina.design.strategy.interfac.Sort; /** * Bubble algorithm * * @author Freedom * */ public class BubbleSort implements Sort { @Override public void sort(int[] array) { int temp = 0; for (int i = 0; i < array.length - 1; i++) { for (int j = 0; j < array.length - 1 - i; j++) { if (array[j] > array[j + 1]) { temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } System.out.println(Arrays.toString(array) + " bubbleSort"); } }
package net.oschina.design.strategy.interfac.impl; import java.util.Arrays; import net.oschina.design.strategy.interfac.Sort; /** * Insertion sort * * @author Freedom * */ public class InsertSort implements Sort { @Override public void sort(int[] array) { for (int i = 1; i < array.length; i++) { int temp = array[i]; int j = i - 1; for (; j >= 0 && array[j] > temp; j--) { // Shift the value greater than temp by one unit as a whole array[j + 1] = array[j]; } array[j + 1] = temp; } System.out.println(Arrays.toString(array) + " insertSort"); } }
2. The interface object of the combined behavior family in the parent class
① Combine an algorithm family interface object in the parent class, the purpose is for the client (subclass) to instantiate and also instantiate the subclass object of a specific algorithm family
②In short, the purpose of the following abstract classes is also for client subclasses to implement concretely. When the client instantiates, it determines the specific algorithm family object.
package net.oschina.design.strategy.abs; import net.oschina.design.strategy.interfac.Sort; /** * Strategy pattern, abstract parent class * * @author Freedom * */ public abstract class SortAbstract { // Sorted properties, interface objects that combine behavior families protected Sort sort; public SortAbstract() { } // call the sort method public void display(int[] array) { sort.sort(array); } }
3. Client subclass
package net.oschina.design.strategy.abs.userimpl; import net.oschina.design.strategy.abs.SortAbstract; import net.oschina.design.strategy.interfac.impl.BubbleSort; /** * Users using bubble sort * * @author Freedom * */ public class BubbleUser extends SortAbstract { public BubbleUser() { sort = new BubbleSort(); } }
The advantages and disadvantages of the strategy mode
1. Advantages
(1) The strategy pattern provides a way to manage related algorithm families. The hierarchy of policy classes defines a family of algorithms or behaviors. Proper use of inheritance can move common code into the parent class, thereby avoiding code duplication.
(2) The strategy mode encapsulates the invoked algorithm as a behavior, which is separated from the user of the specific invocation, which is convenient for later maintenance and expansion.
2. Disadvantages
(1) The client must know all the policy classes and decide which one to use. This means that clients must understand the difference between these algorithms in order to choose the right algorithm class at the right time. In other words, the strategy pattern only works when the client knows the algorithm or behavior.
(2) Since the strategy mode encapsulates each specific strategy implementation as a class, if there are many alternative strategy algorithms, the number of objects will also be large, and there will be many objects combined in the parent class.
4. Summary
1. The specific implementation of the strategy pattern
Respectively encapsulate behavior interfaces and implement behavior families. The interface objects of the combined behavior family are combined in the parent class, and the behavior objects are instantiated in the subclass.
2. Principles of the Strategy Pattern
Separate the changed parts (this change refers to having the same behavior, but the specific implementation is different, such as the sorting algorithm in the above example), encapsulate it as an interface, and write various functions based on the interface. This mode implements the behavior algorithm and the user is independent.
3. Equality between algorithm families
A great feature of the strategy mode is the equality of each strategy algorithm. For a series of specific strategy algorithms, everyone's status is exactly the same. It is because of this equality that algorithms can be replaced with each other. All strategy algorithms (multiple strategy algorithms) are also independent of each other in implementation and have no dependencies on each other.
So this series of policy algorithms can be described as follows: Policy algorithms are different implementations of the same behavior.