Java Strategy Mode)

Source: Internet
Author: User

Java Strategy Mode)
Strategy is an object behavior pattern in design patterns. 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 a business change chart: Line Curve and bar, you can use Strategy. Here we take string substitution as an example. There is a file. After reading it, we want to replace the corresponding variable and then output it. There may be many ways to replace the variables, depending on the user's requirements, so we need to prepare several sets of variable character substitution solutions. Java policy mode first, we create an abstract class RepTempRule to 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 in RepTempRule that needs to be clearly inherited. In this replace, it is actually a specific alternative method. We now have two character substitution schemes: replacing aaa with bbb in text and replacing aaa with ccc in text. The corresponding classes are RepTempRuleOne and RepTempRuleTwo: 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") ;}} public class RepTempRuleTwo extends RepTempRule {public void replace () throws Exception {newString = oldString. replaceFirst ("aaa", "ccc") System. out. println ("This is replace Two") ;}} Step 2: Create an algorithm solution class to provide clients with the freedom to select algorithms. Public class RepTempRuleSolve {private RepTempRule strategy; public RepTempRuleSolve (RepTempRule rule) {this. strategy = rule;} public String getNewContext (Site site Site, String oldString) {return strategy. replace (site, oldString);} public void changeAlgorithm (RepTempRule newAlgorithm) {strategy = newAlgorithm ;}} call the following: public class test {...... public void testReplace () {// use the first alternative RepTempRuleSolve Solver = new RepTempRuleSolve (new RepTempRuleSimple (); solver. getNewContext (site, context); // use the second set of solver = new RepTempRuleSolve (new RepTempRuleTwo (); solver. getNewContext (site, context );}.....} we have achieved the goal of freely switching algorithms during operation. In reality, the core part of Strategy is the use of abstract classes. The Strategy mode can be used with very few modifications and is fast when users need to change. There are some similarities between Strategy and Factory. Strategy is relatively simple and easy to understand, and can switch freely at runtime. Factory focuses on creating objects. Strategy is suitable for the following scenarios: saving files in different formats; compressing files with different algorithms; capturing images with different algorithms; outputting images with the same data in different formats, for example, curve or block bar.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.