Strategy (strategy)

zhaozj2021-02-16  106

Strategy (strategy)

Strategy is a model behavior pattern in design patterns, mainly to define a series of racing algorithms to encapsulate these algorithms into a separate category.

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

Here is a string replacement as an example, there is a file, and we need to read it, it is desirable to replace the corresponding variables and then output. There may be a variety of ways to replace the variables, depending on the user's requirements, so we have to prepare several sets of digital yuan alternatives.

First, we create an abstract category 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 the retemprule to inherit explicit, which is actually an alternative method. We now have two modular alternatives, 1. Alternative to BBB in the text; 2. Replace the AAA in the text into CCC; corresponding categories are reptempruleone reptempruletwo

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");}}

At this time, we have completed the design and programming of the category map. The call is as follows:

Public class test {... public void testReplace () {// uses the first set of schemes to replace. Reptemprule rule = new reptemprue (); rule.setoldstring (record); rule.replace ();} .....}

The actual core part of the entire Strategy is the use of abstract categories. It can be used when users need to change, and quickly.

Strategy and Factory have a certain similar, and Strategy is relatively easy to understand.

Strategy is suitable for the following occasions:

1. Save files in different formats;

2. Compress files in different algorithms;

3. Intercept image with different algorithms;

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

First, we create an abstract category 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 the reptemprule to inherit explicit, which is actually an alternative method in this Replace. We now have two modular alternatives, 1. Alternative to BBB in the text; 2. Replace the AAA in the text into CCC; corresponding categories are reptempruleone reptempruletwo

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");}}

At this time, we have completed the design and programming of the category map. The call is as follows:

Public class test {... public void testReplace () {// uses the first set of schemes to replace. Reptemprule rule = new reptemprue (); rule.setoldstring (record); rule.replace ();} .....}

The actual core part of the entire Strategy is the use of abstract categories. It can be used when users need to change, and quickly.

Strategy and Factory have a certain similar, and Strategy is relatively easy to understand.

Strategy is suitable for the following occasions:

1. Save files in different formats;

2. Compress files in different algorithms;

3. Intercept image with different algorithms;

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

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

New Post(0)