Strategy is an object behavioral pattern in the design pattern. It mainly defines a series of algorithms and encapsulates these algorithms into separate classes one by one.
Stratrgy is widely used. For example, there may be two ways to implement the business change chart of the company, one is the line curve and the other is the block diagram (bar). These are two algorithms, which can be implemented using Strategy.
Here we take string substitution as an example. There is a file that we need to read after it, hope to replace the corresponding variables in it, and then output. There may be multiple ways to replace the variables in it, depending on the user's requirements, so we need to prepare several sets of variable character alternatives.
First, we create an abstract class RepTempRule to define some common variables and methods:
The code copy is as follows:
public abstract class RepTempRule{
protected String oldString="";
public void setOldString(String oldString){
this.oldString=oldString;
}
protected String newString="";
public String getNewString(){
return newString;
}
public abstract void replace() throws Exception;
}
There is an abstract method abstract in RepTempRule that needs to be inherited clearly, and this replace is actually a specific method that replaces it.
We now have two character alternatives:
1. Replace aaa in the text with bbb;
2. Replace aaa in the text with ccc.
The corresponding classes are RepTempRuleOne and RepTempRuleTwo:
The code copy is as follows:
public class RepTempRuleOne extends RepTempRule{
public void replace() throws Exception{
//replaceFirst is a new feature of jdk1.4 newString=oldString.replaceFirst("aaa", "bbbb")
System.out.println("this is replace one");
}
}
The code copy is as follows:
public class RepTempRuleTwo extends RepTempRule{
public void replace() throws Exception{
newString=oldString.replaceFirst("aaa", "ccc")
System.out.println("this is replace Two");
}
}
Step 2: We need to establish an algorithm solution class to provide the client with the ability to freely select algorithms.
The code copy is as follows:
public class RepTempRuleSolve {
private RepTempRule strategy;
public RepTempRuleSolve(RepTempRule rule){
this.strategy=rule;
}
public String getNewContext(Site site,String oldString) {
return strategy.replace(site,oldString);
}
public void changeAlgorithm(RepTempRule newAlgorithm) {
strategy = newAlgorithm;
}
}
The call is as follows:
The code copy is as follows:
public class test{
......
public void testReplace(){
//Use the first set of alternatives RepTempRuleSolve solver=new RepTempRuleSolve(new RepTempRuleSimple());
solver.getNewContext(site,context);
//Use the second set of solvers=new RepTempRuleSolve(new RepTempRuleTwo());
solver.getNewContext(site,context);
}
.....
}
We have achieved the goal of freely switching algorithms during operation.
In fact, the core part of the entire Strategy is the use of abstract classes. Using the Strategy mode can make the number of modifications very small and fast when the user needs to change.
Strategy and Factory are similar. Strategy is relatively simple and easy to understand, and can be switched freely at runtime. Factory focuses on creating objects.
Strategy is suitable for the following occasions:
1. Save the file in different formats;
2. Compress files with different algorithms;
3. Intercept images with different algorithms;
4. Output graphs of the same data in different formats, such as curves or block diagram bars.