Strategy Mode
In Strategy Mode (Strategy Pattern), a class of behavior or algorithm can be changed at runtime. This type of design pattern belongs behavioral patterns.
In strategy mode, we create the context object represents the behavior of various objects and a policy change with the change of policy objects. Policy object to change the algorithm execution context object.
Introduction
It is intended to: define a set of algorithms, encapsulate them one by one, and makes them interchangeable.
The main solution: In a variety of algorithms similar case, if ... else brought complex and difficult to maintain.
When to use: a system has many, many classes, and distinguish them only their immediate behavior.
How to solve: these algorithms into a package of a class, arbitrarily replaced.
The key code: implement the same interface.
Application examples: 1, Zhuge Liang of good ideas, each kit is a strategy. 2, travel way to travel, cycling, by car, each way to travel is a strategy. 3, JAVA AWT in LayoutManager.
Advantages: 1, the algorithm can freely switch. 2, to avoid the use of multiple conditional. 3, good scalability.
Disadvantages: 1, the Strategy will increase. 2, all policy classes need of external exposure.
Usage scenarios: 1, if there is a system in which many classes, the difference between them only in their behavior, then use the strategy pattern allows an object can be dynamically select an action in a number of behavior. 2, a system need to dynamically choose one of several algorithms. 3. If an object has a lot of behavior, if that is not appropriate mode, these acts had to use multiple conditional selection statement to achieve.
Note: If the policy is a system of more than four, we need to consider the use of mixed-mode, problem-solving strategies class expansion.
achieve
We will create a custom activity of Strategy interface and implementation of the Strategy entity policy class interface. Context is using a strategy of some kind.
StrategyPatternDemo , our demonstration classes use Context and policy objects to demonstrate changes in behavior Context when it configuring or using a policy change.
step 1
Create an interface.
Strategy.java
public int doOperation(int num1, int num2);
}
Step 2
Creating an entity class that implements the interface.
OperationAdd.java
@Override
public int doOperation(int num1, int num2) {
return num1 + num2;
}
}
OperationSubstract.java
@Override
public int doOperation(int num1, int num2) {
return num1 - num2;
}
}
OperationMultiply.java
@Override
public int doOperation(int num1, int num2) {
return num1 * num2;
}
}
Step 3
Creating Context class.
Context.java
private Strategy strategy;
public Context(Strategy strategy){
this.strategy = strategy;
}
public int executeStrategy(int num1, int num2){
return strategy.doOperation(num1, num2);
}
}
Step 4
Use Context to see if it changed strategy Strategy changes in behavior.
StrategyPatternDemo.java
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 OperationSubstract());
System.out.println("10 - 5 = " + context.executeStrategy(10, 5));
context = new Context(new OperationMultiply());
System.out.println("10 * 5 = " + context.executeStrategy(10, 5));
}
}
Step 5
The implementation of the program, output:
10 + 5 = 15 10 - 5 = 5 10 * 5 = 50