definition
In Strategy Pattern, the behavior of a class or its algorithm can be changed at run time. This type of design pattern belongs to behavioral pattern.
In the policy pattern, we create objects representing various policies and a context object whose behavior changes with the change of policy objects. The policy object changes the execution algorithm of the context object.
structure
● Context encapsulates roles
It is also called context role, which serves as a connecting link between the preceding and the following, shields the direct access of high-level modules to policies and algorithms, and encapsulates possible changes.
● Strategy Abstract Strategy roles
The abstraction of a policy or algorithm family, usually an interface, defines the methods and attributes that each policy or algorithm must have.
● concrete strategy specific strategic roles (multiple)
Implement the operations in the abstract policy. This class contains specific algorithms.
Usage scenario:
● multiple classes have only slightly different scenarios in algorithm or behavior.
● scenarios where the algorithm needs to switch freely.
● scenarios requiring masking algorithm rules.
Note: if the number of specific strategies exceeds 4, you need to consider using mixed mode
Policy mode extension: Policy enumeration
Class diagram
case
Example: simple version: the policy mode implements addition, subtraction, multiplication and division
// Abstract policy role public interface Strategy { int doOperation(int num1, int num2); } //Specific policy roles public class OperationAdd implements Strategy { @Override public int doOperation(int num1, int num2) { return num1 + num2; } } public class OperationSubtract implements Strategy { @Override public int doOperation(int num1, int num2) { return num1 - num2; } } public class OperationMultiply implements Strategy { @Override public int doOperation(int num1, int num2) { return num1 * num2; } } public class OperationDivide implements Strategy { @Override public int doOperation(int num1, int num2) { return num1 / num2; } } // Context encapsulated role / context role public class Context { private Strategy strategy; public void setStrategy(Strategy strategy) { this.strategy = strategy; } public int executeStrategy(int num1, int num2){ return strategy.doOperation(num1, num2); } } public class StrategyDemo { public static void main(String[] args) { Context context = new Context(); context.setStrategy(new OperationAdd()); System.out.println("OperationAdd=" + context.executeStrategy(6, 2)); context.setStrategy(new OperationSubtract()); System.out.println("OperationSubtract=" + context.executeStrategy(6, 2)); context.setStrategy(new OperationMultiply()); System.out.println("OperationMultiply=" + context.executeStrategy(6, 2)); context.setStrategy(new OperationDivide()); System.out.println("OperationDivide=" + context.executeStrategy(6, 2)); } } //Output: OperationAdd=8 OperationSubtract=4 OperationMultiply=12 OperationDivide=3
summary
The main advantages of the strategy model are as follows.
- Multiple conditional statements are difficult to maintain, and the use of policy mode can avoid the use of multiple conditional statements, such as if... else statements and switch... case statements.
- Policy pattern provides a series of reusable algorithm families. Proper use of inheritance can transfer the public code of the algorithm family to the parent class, so as to avoid repeated code.
- The policy pattern can provide different implementations of the same behavior, and customers can choose different policies according to different time or space requirements.
- The strategy mode provides perfect support for the opening and closing principle, and can flexibly add new algorithms without modifying the original code.
- The policy pattern puts the use of the algorithm into the environment class, and the implementation of the algorithm is moved to the specific policy class, which realizes the separation of the two.
Its main disadvantages are as follows.
- The client must understand the differences between all policy algorithms in order to select the appropriate algorithm class in time.
- The policy mode creates many policy classes, which increases the difficulty of maintenance.
NEXT
Next time, we will analyze the behavior model - responsibility chain model. Welcome to praise, pay attention and collect. Thank you.
Read a hundred times, its meaning is self-evident. I'm 23 months. See you next time.