Strategy is a pattern of object behavior in design patterns, which mainly defines a series of algorithms and encapsulates them into separate classes.
Stratrgy application is more extensive, for example, the company business Change chart, there may be two ways to achieve, one is the line curve, one is the Block Diagram (bar), which is the two algorithms, you can use strategy implementation.
Here, as an example of string substitution, there is a file that we need to read after we want to replace the corresponding variable, and then output. There may be several ways to override a variable, depending on the user's requirements, so we have to prepare several sets of variable character substitution scenarios.
First, we create an abstract class Reptemprule define some common variables and methods:
Copy Code code 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 in Reptemprule. Abstract needs to be explicitly inherited, and this replace is actually a concrete method of substitution.
We now have two character alternatives:
1. Replace triple A In the text with BBB;
2. Replace the text with AAA as the CCC.
The corresponding classes are Reptempruleone and Reptempruletwo respectively:
Copy Code code 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");
}
}
Copy Code code as follows:
public class Reptempruletwo extends reptemprule{
public void replace () throws exception{
Newstring=oldstring.replacefirst ("AAA", "CCC")
System.out.println ("This is replace two");
}
}
The second step: we want to create an algorithm to solve the class, to provide the client can choose the algorithm freely.
Copy Code code 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:
Copy Code code 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
Solver=new Reptemprulesolve (New Reptempruletwo ());
Solver.getnewcontext (Site,context);
}
.....
}
We have reached the goal of switching the algorithm freely during operation.
The core of the actual entire strategy is the use of abstract classes, and the use of strategy patterns can be very small and fast when users need to change.
Strategy and factory have a certain similarity, strategy is relatively simple and easy to understand, and can switch freely at run time. Factory emphasis is used to create objects.
Strategy is suitable for the following occasions:
1. Save the document in a different format;
2. Compress the files with different algorithms;
3. Capture images with different algorithms;
4. Graphics that output the same data in different formats, such as curves or block diagrams bar.