Java implementation Strategy (strategy) mode

Source: Internet
Author: User

Policy mode: Behavioral mode

The same behavior, different processing algorithms are encapsulated separately. So they can replace each other.

1. Define a super-type interface and behavior method

2. Define different implementation classes and implement different algorithms for this behavior


/** * Strategy mode: For the same command (or behavior), different strategies to do different actions *  Product Promotion *  This class is: Cash Collection class *   *  @author Stone */public interface Icashsuper { Double Acceptcash (double money);}

/** * Normal cash charge * @author Stone * */public class Cashnormal implements Icashsuper {@Overridepublic double acceptcash (double m Oney) {return money;}}

/** * Discounted cash charge  * @author Stone * */public class Cashrebate implements Icashsuper {private double rebate;//Discount Public Cash Rebate (double rebate) {this.rebate = rebate;} @Overridepublic Double Acceptcash (double money) {return new BigDecimal (Money * rebate/10). Setscale (2, Bigdecimal.round_ HALF_UP). Doublevalue ();}}

/** * return  cash collected * @author stone * */public class Cashreturn implements Icashsuper {private double moneycondition;//Cashback Bottom amount private double returnmoney; Return amount public Cashreturn (double moneycondition, double returnmoney) {this.moneycondition = moneycondition; This.returnmoney = Returnmoney;} @Overridepublic Double Acceptcash (double money) {//Multiple rebate if (Money >= moneycondition) {return Money-math.floor (Money/ moneycondition) * Returnmoney;} else {return money;}}}

/** * Based on the policy class passed. Run the corresponding behavior * @author Stone * */public class Cashcontext {private Icashsuper casher;public Cashcontext () {}public Cashcontext ( Icashsuper casher) {this.casher = Casher;} public void Setcasher (Icashsuper casher) {this.casher = Casher;} According to the detailed policy object, the algorithm that invokes it behaves public double acceptcash (double money) {return This.casher.acceptCash (money);}}

/* * Policy (Strategy) mode of concern: the choice of behavior * Encapsulates a series of policy objects that the user chooses to use for the difference between a simple factory and a single one: * Policy mode. The incoming policy object is given to the context, which is called by the context wrapper policy object's method invocation, exposing the context's method interface * Simple Factory mode, passing in a simple argument. Create an object and then call the Factory object's method * different from the adornment mode: * Very obvious. Context does not need to implement (implements) the business interface, do not need to enhance the functionality of existing policy objects * Policy mode is used in algorithmic decision systems, such as Payroll */public class Test {public static void main (String [] args) {double money = 998;//original price Cashcontext Cashcontext = new Cashcontext (new Cashnormal ()); System.out.println ("Original price:" + cashcontext.acceptcash (money)); Usually the  strategy Cashcontext.setcasher (new Cashrebate (8.5)); System.out.println ("Hit 85 percent:" + cashcontext.acceptcash (money)); Discount   Strategy   85 percent Cashcontext.setcasher (new Cashreturn (300, 50)); SYSTEM.OUT.PRINTLN ("Full 300 Rebate:" + Cashcontext.acceptcash (money)); Rebate  policy    full 300 return 50}}

Print

Original Price: 998.0 dozen 85 Percent: 848.3 full 300 return 50:848


Java implementation Strategy (strategy) mode

Related Article

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.