// 现金收费抽象类public abstract class CashSuper { public abstract double acceptCash (Double money);}// 此类是正常付款,没有任何折扣返利。。。。public class CashNormal extends CashSuper { public double acceptCash(Double money) { return money; }}// 此类是对各种打折抽象public class CashRebate extends CashSuper{ private String moneyRebate; double moneyRebate_d; public CashRebate(String moneyRebate) { moneyRebate_d = Double.parseDouble(moneyRebate); } public double acceptCash(Double money) { return money * moneyRebate_d; }}// 满减public class CashReturn extends CashSuper{ // 300 -100 private double moneyCondition; private double moenyReturn; public CashReturn(String moneyCondition, String moenyReturn) { super(); this.moneyCondition = Double.parseDouble(moneyCondition); this.moenyReturn = Double.parseDouble(moenyReturn) ; } public double acceptCash(Double money) { if(money>moneyCondition) { return money-Math.floor(money/moneyCondition)*moenyReturn; } return money; }}// 策略模式与工厂模式的相结合public class CashContext { CashSuper cashSuper; private String type; public CashContext(String type) { this.type=type; switch (type) { case "正常": cashSuper=new CashNormal(); break; case "三折": cashSuper=new CashRebate("0.8"); break; case "返现金": cashSuper=new CashReturn("300","100"); break; default: break; } } public double getResult(Double money) { return cashSuper.acceptCash(money); }}