Strategy (strategy)

xiaoxiao2021-03-06  81

The Strategy Policy Mode is an object behavior pattern in the design mode, mainly to define a series of algorithms, and package these algorithms into a separate class.

The StratRGY app is relatively wide, such as the company's business changes, there may be two implementations, one is the line curve, one is a block diagram (BAR), this is two algorithms, you can use the Strategy implementation.

Here is a string, there is a file, and after we need to read, you want to replace the corresponding variables, then output. There are many ways to replace the variable, depending on the user's requirements, so we want Prepare several sets of variable characters alternatives.

First, we build an abstract class reptemprule define some public variables and methods:

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, Abstract is clear, and this Replace is actually an alternative method. We now have two character alternatives, 1. Alternative to BBB in the text; 2. Alternative to CCC in the text The corresponding class is the reptempruleone reptempruletwo, respectively.

public class RepTempRuleOne extends RepTempRule {public void replace () throws Exception {// replaceFirst new feature is jdk1.4 newString = oldString.replaceFirst ( "aaa", "bbbb") System.out.println ( "this is replace one") }}

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 want to establish an algorithm to solve the class, used to provide the client to freely select the algorithm.

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:

Public class test {... public void testReplace () {// Using the first set of alternatives reptemprulesolve solver = new reptemprulesolve (new reptemprulesimple ()); Solver.getNewContext (site, context); // Using the second Set Solver = New ReptemPRulesolve (New ReptemPRuletwo ()); Solver.getNewContext (Site, Context);} .....} We reach the purpose of freely switching the algorithm during operation.

The actual core part of the entire Strategy is the use of abstract classes. Using the Strategy mode, the modified amount can be rare, and fast.

Strategy and Factory have a certain similar, Strategy is relatively easy to understand, and can freely switch at runtime. Factory focus is used to create objects.

Strategy is suitable for the following occasions:

1. Save files in different formats;

2. Compress files in different algorithms;

3. Intercept image in different algorithms;

4. Output graphics of the same data in different formats, such as curve or block diagram bar, etc.

转载请注明原文地址:https://www.9cbs.com/read-93060.html

New Post(0)