The strategy mode mainly defines a series of algorithms, the friends who have learned the data structure must know that there are many algorithms for the array from big to small, such as bubbling, exchanging, fast inserting and so on, the strategy pattern is to encapsulate these algorithms into separate classes, which makes it easy to make arbitrary calls. Here is a string substitution example, there is a file that we need to read after, want to replace the corresponding variable, and then output. There are several ways to replace variables, depending on the user's requirements, so we'll prepare several sets of variable character alternatives.
First, we create an abstract class Reptemprule to define some common variables and methods:
Public abstract class reptemprule{protected String oldstring "; protected String newstring; public void setoldstring (String oldstring) {this.oldstring=oldstring;} Public String getnewstring () {return newstring;} public abstract void Replace () throws Exception; }
In Reptemprule, there is an abstract method that requires explicit inheritance, this substitution is actually an alternative to the specific method, the following we want to inherit the abstract class, implementation of its specific replacement algorithm. Let's assume that there are now two character alternatives, 1. Replace the AAA in the text with a BBB; 2. Replace the AAA in the text with the CCC; the corresponding classes are Reptempruleone Reptempruletwo
public class Reptempruleone extends reptemprule{public void replace () throws exception{ newstring= Oldstring.replacefirst ("AAA", "BBBB") //replacefirst is a method System.out.println the JDK ("This is replace one");}}
Step two: We are going to create an algorithm solution class that provides the client with the option to freely select the algorithm.
public class Reptemprulesolve {private Reptemprule strategy;public reptemprulesolve (reptemprule rule) { //constructor method This.strategy=rule;} Public String getnewcontext (Site site,string oldstring) { return strategy.replace (site,oldstring);} Change algorithm public void Changealgorithm (Reptemprule newalgorithm) { strategy = Newalgorithm;}}
The third step is to execute the algorithm
Reptemprulesolve solver=new reptemprulesolve (New Reptemprulesimple ()); Solver.getnewcontext (Site,context);//using the second set of Solver=new reptemprulesolve (New Reptempruletwo ()); Solver.getnewcontext (Site,context);
The core part of the actual whole strategy is the use of abstract class, using strategy mode can be changed when the user needs to change the amount of small, and fast.
Strategy and factory have a certain similarity, the strategy is relatively simple and easy to understand, and can be switched freely at run time. Factory focus is used to create objects.
The strategy is suitable for the following occasions:
1. Save the file in a different format;
2. Compress the files with different algorithms;
3. Interception of images with different algorithms;
4. Graphs that output the same data in different formats, such as curves or block diagrams bar, etc.