策略模式+工廠模式優化if...else if...else if結構
阿新 • • 發佈:2019-01-07
首先,按照if...else if語句來實現打折商品的例子,程式碼如下:
public class Example { public Double calRecharge(Double charge ,RechargeTypeEnum type ){ if(type.equals(RechargeTypeEnum.E_BANK)){ return charge*0.85; }else if(type.equals(RechargeTypeEnum.BUSI_ACCOUNTS)){ return charge*0.90; }else if(type.equals(RechargeTypeEnum.MOBILE)){ return charge; }else if(type.equals(RechargeTypeEnum.CARD_RECHARGE)){ return charge+charge*0.01; }else{ return null; } } }
用策略+工廠模式優化:
列舉類:
策略介面:package strategy; public enum RechargeTypeEnum { E_BANK(1, "網銀"), BUSI_ACCOUNTS(2, "商戶賬號"), MOBILE(3,"手機卡充值"), CARD_RECHARGE(4,"充值卡"); private int value; private String description; private RechargeTypeEnum(int value, String description) { this.value = value; this.description = description; } public int value() { return value; } public String description() { return description; } public static RechargeTypeEnum valueOf(int value) { for(RechargeTypeEnum type : RechargeTypeEnum.values()) { if(type.value() == value) { return type; } } return null; } }
package strategy.strategy;
import strategy.RechargeTypeEnum;
public interface Strategy {
public Double calRecharge(Double charge ,RechargeTypeEnum type );
}
策略具體實現類:
package strategy.strategy; import strategy.RechargeTypeEnum; public class EBankStrategy implements Strategy{ @Override public Double calRecharge(Double charge, RechargeTypeEnum type) { return charge*0.85; } }
package strategy.strategy;
import strategy.RechargeTypeEnum;
public class BusiAcctStrategy implements Strategy{
@Override
public Double calRecharge(Double charge, RechargeTypeEnum type) {
// TODO Auto-generated method stub
return charge*0.90;
}
}
package strategy.strategy;
import strategy.RechargeTypeEnum;
public class MobileStrategy implements Strategy {
@Override
public Double calRecharge(Double charge, RechargeTypeEnum type) {
// TODO Auto-generated method stub
return charge;
}
}
package strategy.strategy;
import strategy.RechargeTypeEnum;
public class CardStrategy implements Strategy{
@Override
public Double calRecharge(Double charge, RechargeTypeEnum type) {
return charge+charge*0.01;
}
}
策略上下文:
package strategy.strategy;
import strategy.RechargeTypeEnum;
public class Context {
private Strategy strategy;
public Double calRecharge(Double charge, Integer type) {
strategy = StrategyFactory.getInstance().creator(type);
return strategy.calRecharge(charge, RechargeTypeEnum.valueOf(type));
}
public Strategy getStrategy() {
return strategy;
}
public void setStrategy(Strategy strategy) {
this.strategy = strategy;
}
}
工廠類:
package strategy.strategy;
import java.util.HashMap;
import java.util.Map;
import strategy.RechargeTypeEnum;
public class StrategyFactory {
private static StrategyFactory factory = new StrategyFactory();
private StrategyFactory(){
}
private static Map strategyMap = new HashMap<>();
static{
strategyMap.put(RechargeTypeEnum.E_BANK.value(), new EBankStrategy());
strategyMap.put(RechargeTypeEnum.BUSI_ACCOUNTS.value(), new BusiAcctStrategy());
strategyMap.put(RechargeTypeEnum.MOBILE.value(), new MobileStrategy());
strategyMap.put(RechargeTypeEnum.CARD_RECHARGE.value(), new CardStrategy());
}
public Strategy creator(Integer type){
return strategyMap.get(type);
}
public static StrategyFactory getInstance(){
return factory;
}
}
客戶端類:
package strategy.strategy;
import strategy.RechargeTypeEnum;
public class Client {
public static void main(String[] args) {
Context context = new Context();
// 網銀充值100 需要付多少
Double money = context.calRecharge(100D,
RechargeTypeEnum.E_BANK.value());
System.out.println(money);
// 商戶賬戶充值100 需要付多少
Double money2 = context.calRecharge(100D,
RechargeTypeEnum.BUSI_ACCOUNTS.value());
System.out.println(money2);
// 手機充值100 需要付多少
Double money3 = context.calRecharge(100D,
RechargeTypeEnum.MOBILE.value());
System.out.println(money3);
// 充值卡充值100 需要付多少
Double money4 = context.calRecharge(100D,
RechargeTypeEnum.CARD_RECHARGE.value());
System.out.println(money4);
}
}
執行結果:
85.0
90.0
100.0
101.0
從上面程式碼可以看出,策略模式把具體的演算法封裝到了具體策略角色內部,增強了可擴充套件性,隱蔽了實現細節;它替代繼承來實現,避免了if- else這種不易維護的條件語句。當然我們也可以看到,策略模式由於獨立策略實現,使得系統內增加了很多策略類;對客戶端來說必須知道兜友哪些具體策略, 而且需要知道選擇具體策略的條件。