Java policy pattern
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.
introduce
Intent: define a series of algorithms, encapsulate them one by one, and make them replaceable.
Main solution: when there are many similar algorithms, it is complex and difficult to maintain when using if...else.
When to use: a system has many classes, and what distinguishes them is their direct behavior.
How to solve it: encapsulate these algorithms into classes one by one and replace them arbitrarily.
Key code: implement the same interface.
Application example: ① Zhuge Liang's clever plan, each bag is a strategy. ② The way of travel, choose to ride a bike or take a car. Each way of travel is a strategy. ③ LayoutManager in JAVA AWT.
advantage: ① The algorithm can be switched freely. ② Avoid using multiple conditional judgments. ③ Good scalability.
Disadvantages: ① Policy classes will increase. ② All policy classes need to be exposed.
Usage scenario: ① If there are many classes in a system, and the only difference between them is their behavior, then using the policy pattern can dynamically let an object choose one behavior among many behaviors. ② A system needs to dynamically choose one of several algorithms. ③ If an object has a lot of behaviors, without appropriate patterns, these behaviors have to be implemented by multiple conditional selection statements.
Note: if a system has more than four policies, you need to consider using mixed mode to solve the problem of policy class expansion.
realization
We will create a Strategy interface that defines the activity and an entity policy class that implements the Strategy interface. Context is a class that uses a certain policy.
StrategyPatternDemo, our presentation class uses Context and policy objects to demonstrate the behavior change of Context when the policy it configures or uses changes.
Step 1
Create an interface.
public interface Strategy { public int doOperation(int num1, int num2); } Copy code
Step 2
Create an entity class that implements the interface.
public class OperationAdd implements Strategy{ @Override public int doOperation(int num1, int num2) { return num1 + num2; } } Copy code
public class OperationSubtract implements Strategy{ @Override public int doOperation(int num1, int num2) { return num1 - num2; } } Copy code
public class OperationMultiply implements Strategy{ @Override public int doOperation(int num1, int num2) { return num1 * num2; } } Copy code
Step 3
Create a Context class.
public class Context { private Strategy strategy; public Context(Strategy strategy){ this.strategy = strategy; } public int executeStrategy(int num1, int num2){ return strategy.doOperation(num1, num2); } } Copy code
Step 4
Use Context to see the behavior changes when it changes the Strategy.
public class StrategyPatternDemo { public static void main(String[] args) { Context context = new Context(new OperationAdd()); System.out.println("10 + 5 = " + context.executeStrategy(10, 5)); context = new Context(new OperationSubtract()); System.out.println("10 - 5 = " + context.executeStrategy(10, 5)); context = new Context(new OperationMultiply()); System.out.println("10 * 5 = " + context.executeStrategy(10, 5)); } } Copy code
Step 5
Execute the program and output the results:
10 + 5 = 15 10 - 5 = 5 10 * 5 = 50