First, demand
According to vip user to return different price levels, vip level is not fixed, may at any time to increase, the price is not fixed.
Second, the conventional wording
/ ** * If you have a new type, you need to frequently modify the code here! * Does not comply with the principle of opening and closing! * @Author DUCHONG * * / public class CommonGetPrice { public static Final String VIPl = "V1" ; public static Final String VIP2 = "V2" ; public static Final String VIP3 = "V3" ; public static Final String The VIP4 = "V4" ; / ** * get the vip price * @param of the type vip type * @param . price original price *@return * / public static Double getVipPrice (of the type String, Double . price) { IF (type.equals (VIP1)) { System.out.println ( "do not use the strategy pattern --- not discount the original price" ); return . price; } the else IF (type.equals (VIP2)) { System.out.println ( "do not use the strategy pattern --- a discount" ); return . price * 0.9 ; } the else IF (type.equals (VIP3)) { System .out.println ( "policies are not used to play mode --- 15%" ); return . price * 0.85; } The else IF (type.equals (The VIP4)) { System.out.println ( "do not use policy mode --- discount of twenty" ); return . Price * 0.8 ; } return . Price; } }
Third, using the strategy pattern
3.1, the interface to define policies
/** * 策略接口 * @author DUCHONG */ public interface Strategy { double getVipPrice(double originPrice); }
3.2, the definition of context
Context-hold strategy interface reference, the algorithm and the caller isolation.
/ ** * Class responsible for specific strategies and interaction * In this case, the specific algorithms and direct client calls the separation, and makes the algorithm independent of the client independent of changes in the end. * If the spring dependency injection function, the profile can also, different injection strategies dynamic objects, dynamic switching of different algorithms. * @Author DUCHONG * * / public class VipContext { / ** * algorithm currently used by the object * / Private Strategy Strategy; public VipContext () { } / ** * may be injected through the constructor * / public VipContext (Strategy Strategy) { Super (); the this .strategy = Strategy; } / ** * may be set by a method injection * / public void setStrategy(Strategy strategy) { this.strategy = strategy; } public double getVipPrice(double originPrice){ return strategy.getVipPrice(originPrice); } }
3.3, different algorithms class
/ ** * @author DUCHONG * @Since 2019-09-24 18:28 * * / public class VipOneStrategy the implements Strategy { / ** * VIPl not discount * @param originPrice * @return * / @Override public Double getVipPrice ( Double originPrice) { System.out.println ( "use mode --- no discount strategy, the original" ); return originPrice; } } / ** * @author DUCHONG *@since 2019-09-24 18:28 **/ public class VipTwoStrategy implements Strategy{ @Override public double getVipPrice(double originPrice) { System.out.println("使用策略模式---打九折"); return originPrice * 0.9; } } /** * @author DUCHONG * @since 2019-09-24 18:28 **/ public class VipThreeStrategy implements Strategy{ @Override public Double getVipPrice ( Double originPrice) { System.out.println ( "policy mode using 15% --- hit" ); return originPrice 0.85 * ; } } / ** * @author DUCHONG * @Since 2019-09-24 18 is : 28 * * / public class VipFourStrategy the implements at strategy { @Override public Double getVipPrice ( Double originPrice) { System.out.println ( "using policy mode --- discount of twenty percent" ); return originPrice * 0.8; } }
Four, Client
public class Client { public static void main(String[] args) { double originPrice=1000D; //使用策略模式之前 System.out.println(CommonGetPrice.getVipPrice(CommonGetPrice.VIP1,originPrice)); System.out.println(CommonGetPrice.getVipPrice(CommonGetPrice.VIP2,originPrice)); System.out.println(CommonGetPrice.getVipPrice(CommonGetPrice.VIP3,originPrice)); System.out.println(CommonGetPrice.getVipPrice(CommonGetPrice.VIP4,originPrice)); //使用策略模式之后 VipContext vipContext=new VipContext(); Strategy v1=new VipOneStrategy(); vipContext.setStrategy(v1); System.out.println(vipContext.getVipPrice(originPrice)); Strategy v2=new VipTwoStrategy(); vipContext.setStrategy(v2); System.out.println(vipContext.getVipPrice(originPrice)); Strategy v3=new VipThreeStrategy(); vipContext.setStrategy(v3); System.out.println(vipContext.getVipPrice(originPrice)); Strategy v4=new VipFourStrategy(); vipContext.setStrategy(v4); System.out.println(vipContext.getVipPrice(originPrice)); } }
Fifth, the results