策略模式是一种定义一系列算法的方法,以相同的方式调用不同的算法,减少了各种算法与使用算法类之间的耦合
用法:只要在需求分析中需要在不同时间应用不同的业务规则,就可以考虑用策略模式处理变化的可能性
package org.mode.strategy;
import org.mode.factory.CashNormal;
import org.mode.factory.CashRebate;
import org.mode.factory.CashReturn;
import org.mode.factory.CashSuper;
/**
* 策略控制器
*
* @Description: TODO(这里用一句话描述这个类的作用)
* @author Mr.BigDog
* @date 2017年11月11日 下午12:00:52
*/
public class CashContext {
CashSuper cs = null;
public CashContext(String type) {
switch (type) {
case "normal":
CashNormal cs0 = new CashNormal();
cs = cs0;
break;
case "300返100":
CashReturn cs1 = new CashReturn(300, 100);
cs = cs1;
break;
case "0.8":
CashRebate cs2 = new CashRebate(0.8);
cs = cs2;
break;
}
}
public double getResult(double money){
return cs.acceptCash(money);
}
}