/** * 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;}}}
/** * According to the policy class passed, perform the corresponding behavior * @author Stone * */public class Cashcontext {private Icashsuper casher;public Cashcontext () {}pub Lic Cashcontext (Icashsuper casher) {this.casher = Casher;} public void Setcasher (Icashsuper casher) {this.casher = Casher;} According to the specific 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, the user chooses which policy object to use is different from the Simple factory: * Policy mode, incoming policy object to the context, by the context wrapper method call of the policy object, External public Context Method interface * Simple Factory mode, passing in a simple parameter, creating an object, and then calling the factory object's method * differs from the adornment mode: * It is clear that the context does not need to implement the (implements) business interface and does not require the ability to enhance an existing policy object * Policy patterns are used in algorithmic decision systems, such as payroll settlement */public class Test {public static void main (string[] args) {double money = 998;//original Price Cashcontext CA Shcontext = 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