“This is the 27th day of my participation in the Gwen Challenge in November. See details of the event: The Last Gwen Challenge in 2021”

Java Policy Pattern

In the Strategy Pattern, the behavior of a class or its algorithm can be changed at run time. This type of design pattern is behavioral.

In the policy pattern, 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.

introduce

Intent: Define a set of algorithms, encapsulate them one by one, and make them interchangeable.

In the case of similar algorithms, if… Else is complex and difficult to maintain.

When to use: A system has many, many classes that are distinguished only by their immediate behavior.

How to solve it: Encapsulate these algorithms into classes and replace them arbitrarily.

Key code: implement the same interface.

Application example: ① Zhuge Liang’s advice, every advice is a strategy. ② The way to travel, choose to ride a bicycle, by car, each kind of travel is a strategy. ③ LayoutManager in JAVA AWT.

Advantages: (1) The algorithm can be switched freely. ② Avoid using multiple judgment conditions. ③ Good expansibility.

Disadvantages: (1) The policy class will increase. ② All policy classes need to be exposed.

Use scenario: ① If there are many classes in a system, which are distinguished only by 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 many behaviors, without proper patterns, these behaviors will have to be implemented using 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 inflation.

implementation

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 some kind of strategy.

StrategyPatternDemo, our demo class uses Context and policy objects to demonstrate how the Context behaves when the policy it is configured or used changes.

Step 1

Create an interface.

public interface Strategy {
   public int doOperation(int num1, int num2);
}
Copy the 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 the code
public class OperationSubtract implements Strategy{ @Override public int doOperation(int num1, int num2) { return num1 - num2; }}Copy the code
public class OperationMultiply implements Strategy{ @Override public int doOperation(int num1, int num2) { return num1 * num2; }}Copy the code

Step 3

Create the 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 the code

Step 4

Use Context to see the behavior change when it changes the Strategy 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 the code

Step 5

Execute the program and output the result:

10 plus 5 is 15. 10 minus 5 is 5. 10 times 5 is 50Copy the code