[大话设计模式]学习笔记——策略模式
需求:
![](https://images2015.cnblogs.com/blog/891006/201604/891006-20160407134754390-1966028504.jpg)
简单工厂模式实现:
1.收费方式抽象类
public abstract class CashSuper { /** * 现金收取方式的抽象方法 * @param money 原价 * @return 当前价 */ protected abstract double acceptCash(double money); }
2.普通收费模式
public class CashNormal extends CashSuper { @Override protected double acceptCash(double money) { return money; } }
3.打折收费子类
public class CashRebate extends CashSuper { private final double moneyRebate; public CashRebate(String moneyRebate){ this.moneyRebate = Double.parseDouble(moneyRebate); } @Override protected double acceptCash(double money) { return money*moneyRebate; } }
4.返利收费子类
public class CashReturn extends CashSuper { private final double moneyReturn; private final double moneyCondition; public CashReturn(String moneyCondition, String moneyReturn){ this.moneyCondition = Double.parseDouble(moneyCondition); this.moneyReturn = Double.parseDouble(moneyReturn); } @Override protected double acceptCash(double money) { double result = money; if(money>=moneyCondition){ result = money-(money/moneyCondition)*moneyReturn; } return result; } }
5.工厂类
public class CashFactory { public static CashSuper createCashAccept(String type){ CashSuper cashSuper = null; switch (type) { case "正常收费": CashNormal cashNormal = new CashNormal(); cashSuper = cashNormal; break; case "满300减100": CashReturn cashReturn = new CashReturn("300", "100"); cashSuper = cashReturn; break; case "打7折": CashRebate cashRebate = new CashRebate("0.7"); cashSuper = cashRebate; break; } return cashSuper; } }
6.客户端代码
public void btnOk_Click(View view){ CashSuper cashNomal = CashFactory.createCashAccept("正常收费"); double result1 = cashNomal.acceptCash(12.5 * 5); }
代码结构图:
![](https://images2015.cnblogs.com/blog/891006/201604/891006-20160407134755328-1695189636.jpg)
策略模式:
让算法的变化,不会影响到使用算法的变化
![](https://images2015.cnblogs.com/blog/891006/201604/891006-20160407134755890-1207437252.jpg)
![](https://images2015.cnblogs.com/blog/891006/201604/891006-20160407134756422-673042020.jpg)
![](https://images2015.cnblogs.com/blog/891006/201604/891006-20160407134757250-993632376.jpg)
![](https://images2015.cnblogs.com/blog/891006/201604/891006-20160407134757843-702124651.jpg)
![](https://images2015.cnblogs.com/blog/891006/201604/891006-20160407134758265-942498279.jpg)
![](https://images2015.cnblogs.com/blog/891006/201604/891006-20160407134758750-764609639.jpg)
客户端代码
![](https://images2015.cnblogs.com/blog/891006/201604/891006-20160407134759203-873209359.jpg)
策略模式和简单工厂模式结合实现
CashSuper及其3个子类都是不用变
![](https://images2015.cnblogs.com/blog/891006/201604/891006-20160407134759578-664459087.jpg)
1.cashContext类 策略模式和简单工厂模式的结合
public class CashContext { CashSuper cashSuper = null; public CashContext(String type){ cashSuper = null; switch (type) { case "正常收费": CashNormal cashNormal = new CashNormal(); cashSuper = cashNormal; break; case "满300减100": CashReturn cashReturn = new CashReturn("300", "100"); cashSuper = cashReturn; break; case "打7折": CashRebate cashRebate = new CashRebate("0.7"); cashSuper = cashRebate; break; } } public double getResult(double money) { double cashResult = cashSuper.acceptCash(money); return cashResult; } }
2.客户端代码变为
public void btnOk_Click(View view){ CashContext cashContext = new CashContext("正常收费"); double result = cashContext.getResult(12.5 * 5); }
分析:
对比简单工厂设计模式
![](https://images2015.cnblogs.com/blog/891006/201604/891006-20160407134800093-474214188.jpg)
策略模式解析:
![](https://images2015.cnblogs.com/blog/891006/201604/891006-20160407134800750-63994288.jpg)
![](https://images2015.cnblogs.com/blog/891006/201604/891006-20160407134801359-363447137.jpg)