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 policy mode, we create objects that represent various policies and a context object whose behavior changes as the policy object changes. The policy object changes the execution algorithm of the context object. 意图: Define a series of algorithms, encapsulate them one by one, and make them interchangeable. 主要解决: When many algorithms are similar, the complexity and difficulty of using if…else is difficult to maintain. 何时使用: There are many classes in a system, and what distinguishes them is their direct behavior. 如何解决: Encapsulate these algorithms into a class and replace them arbitrarily. 关键代码: Implement the same interface. 应用实例: 1. Zhuge Liang’s ingenious plan, each bag is a strategy. 2, the way to travel, choose to ride a bike, take a car, each way of travel is a strategy. 3. LayoutManager in JAVA AWT. 优点: 1. The algorithm can be switched freely. 2. Avoid using multiple conditional judgments. 3. Good expansibility. 缺点: 1. The number of policy classes will increase. 2. All policy classes need to be exposed. 使用场景: 1. If there are many classes in a system, and the only difference between them is their behavior, then using the policy pattern can dynamically make an object choose one behavior among many behaviors. 2. A system needs to choose one of several algorithms dynamically. 3. If an object has a lot of behavior, if the appropriate pattern is not used, these behaviors have to be realized by using multiple conditional selection statements. 注意事项: If a system has more than four policies, you need to consider using a mixed mode to solve the problem of inflated policy classes. We will create a that defines the activity Strategy Interface and implementation of the Strategy The entity policy class of the interface. Context Is a class that uses some kind of strategy. StrategyPatternDemo Our demo class uses the Context And policy objects to demonstrate how Context behaves when the policy it configures or uses changes. Create an interface. Create an entity class that implements the interface. Create Context Class. Use Context To see when it changes strategy Strategy A change in behavior at the time. Execute the program and output the result: 6.25.1. Introduction ¶
6.25.2. Realize ¶

6.25.3. Step 1 ¶
Strategy.java ¶
publicinterfaceStrategy{publicintdoOperation(intnum1,intnum2);}
6.25.4. Step 2 ¶
OperationAdd.java ¶
publicclassOperationAddimplementsStrategy{@OverridepublicintdoOperation(intnum1,intnum2){returnnum1+num2;}}
OperationSubtract.java ¶
publicclassOperationSubtractimplementsStrategy{@OverridepublicintdoOperation(intnum1,intnum2){returnnum1-num2;}}
OperationMultiply.java ¶
publicclassOperationMultiplyimplementsStrategy{@OverridepublicintdoOperation(intnum1,intnum2){returnnum1\*num2;}}
6.25.5. Step 3 ¶
Context.java ¶
publicclassContext{privateStrategystrategy;publicContext(Strategystrategy){this.strategy=strategy;}publicintexecuteStrategy(intnum1,intnum2){returnstrategy.doOperation(num1,num2);}}
6.25.6. Step 4 ¶
StrategyPatternDemo.java ¶
publicclassStrategyPatternDemo{publicstaticvoidmain(String[]args){Contextcontext=newContext(newOperationAdd());System.out.println("10
+ 5
="+context.executeStrategy(10,5));context=newContext(newOperationSubtract());System.out.println("10
- 5
="+context.executeStrategy(10,5));context=newContext(newOperationMultiply());System.out.println("10
\* 5 ="+context.executeStrategy(10,5));}}
6.25.7. Step 5 ¶
10 + 5 = 15
10 - 5 = 5
10 * 5 = 50