策略模式+工厂模式优化if...else if...else if结构

转载自:http://blog.csdn.net/u011507568/article/details/70238491


首先,按照if...else if语句来实现打折商品的例子,代码如下:

[java]  view plain  copy
  1. public class Example {  
  2.    
  3.      
  4.     public Double calRecharge(Double charge ,RechargeTypeEnum type ){  
  5.         
  6.        if(type.equals(RechargeTypeEnum.E_BANK)){  
  7.            return charge*0.85;  
  8.        }else if(type.equals(RechargeTypeEnum.BUSI_ACCOUNTS)){  
  9.            return charge*0.90;  
  10.        }else if(type.equals(RechargeTypeEnum.MOBILE)){  
  11.            return charge;  
  12.        }else if(type.equals(RechargeTypeEnum.CARD_RECHARGE)){  
  13.            return charge+charge*0.01;  
  14.        }else{  
  15.            return null;  
  16.        }  
  17.    
  18.     }  
  19.      
  20. }  

用策略+工厂模式优化:

枚举类:

[java]  view plain  copy
  1. package strategy;  
  2.    
  3. public enum RechargeTypeEnum {  
  4.    
  5.     E_BANK(1"网银"),  
  6.      
  7.     BUSI_ACCOUNTS(2"商户账号"),  
  8.      
  9.     MOBILE(3,"手机卡充值"),  
  10.      
  11.     CARD_RECHARGE(4,"充值卡");  
  12.      
  13.     private int value;  
  14.      
  15.     private String description;  
  16.      
  17.     private RechargeTypeEnum(int value, String description) {  
  18.        this.value = value;  
  19.        this.description = description;  
  20.     }  
  21.         
  22.     public int value() {  
  23.        return value;  
  24.     }  
  25.     public String description() {  
  26.        return description;  
  27.     }  
  28.      
  29.     public static RechargeTypeEnum valueOf(int value) {  
  30.         for(RechargeTypeEnum type : RechargeTypeEnum.values()) {  
  31.             if(type.value() == value) {  
  32.                 return type;  
  33.             }  
  34.         }  
  35.         return null;  
  36.     }  
  37. }  
策略接口:

[java]  view plain  copy
  1. package strategy.strategy;  
  2.    
  3. import strategy.RechargeTypeEnum;  
  4.    
  5.    
  6. public interface Strategy {  
  7.    
  8.      
  9.     public Double calRecharge(Double charge ,RechargeTypeEnum type );  
  10. }  

策略具体实现类:

[java]  view plain  copy
  1. package strategy.strategy;  
  2.    
  3. import strategy.RechargeTypeEnum;  
  4.    
  5. public class EBankStrategy implements Strategy{  
  6.    
  7.     @Override  
  8.     public Double calRecharge(Double charge, RechargeTypeEnum type) {  
  9.        return charge*0.85;  
  10.     }  
  11.    
  12. }  

[java]  view plain  copy
  1. package strategy.strategy;  
  2.    
  3. import strategy.RechargeTypeEnum;  
  4.    
  5. public class BusiAcctStrategy implements Strategy{  
  6.    
  7.     @Override  
  8.     public Double calRecharge(Double charge, RechargeTypeEnum type) {  
  9.        // TODO Auto-generated method stub  
  10.        return charge*0.90;  
  11.     }  
  12.    
  13. }  
[java]  view plain  copy
  1. package strategy.strategy;  
  2.    
  3. import strategy.RechargeTypeEnum;  
  4.    
  5. public class MobileStrategy implements Strategy {  
  6.    
  7.     @Override  
  8.     public Double calRecharge(Double charge, RechargeTypeEnum type) {  
  9.        // TODO Auto-generated method stub  
  10.        return charge;  
  11.     }  
  12.    
  13. }  
[java]  view plain  copy
  1. package strategy.strategy;  
  2.    
  3. import strategy.RechargeTypeEnum;  
  4.    
  5. public class CardStrategy implements Strategy{  
  6.    
  7.     @Override  
  8.     public Double calRecharge(Double charge, RechargeTypeEnum type) {  
  9.        return charge+charge*0.01;  
  10.     }  
  11.    
  12. }   

策略上下文:

[java]  view plain  copy
  1. package strategy.strategy;  
  2.    
  3. import strategy.RechargeTypeEnum;  
  4.    
  5.    
  6. public class Context {  
  7.    
  8.     private Strategy strategy;  
  9.      
  10.     public Double calRecharge(Double charge, Integer type) {  
  11.        strategy = StrategyFactory.getInstance().creator(type);  
  12.        return strategy.calRecharge(charge, RechargeTypeEnum.valueOf(type));  
  13.     }  
  14.    
  15.     public Strategy getStrategy() {  
  16.        return strategy;  
  17.     }  
  18.    
  19.     public void setStrategy(Strategy strategy) {  
  20.        this.strategy = strategy;  
  21.     }  
  22.      
  23. }  

工厂类:

[java]  view plain  copy
  1. package strategy.strategy;  
  2.    
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.    
  6. import strategy.RechargeTypeEnum;  
  7.    
  8. public class StrategyFactory {  
  9.    
  10.     private static StrategyFactory factory = new StrategyFactory();  
  11.     private StrategyFactory(){  
  12.     }  
  13.     private static Map strategyMap = new HashMap<>();  
  14.     static{  
  15.        strategyMap.put(RechargeTypeEnum.E_BANK.value(), new EBankStrategy());  
  16.        strategyMap.put(RechargeTypeEnum.BUSI_ACCOUNTS.value(), new BusiAcctStrategy());  
  17.        strategyMap.put(RechargeTypeEnum.MOBILE.value(), new MobileStrategy());  
  18.        strategyMap.put(RechargeTypeEnum.CARD_RECHARGE.value(), new CardStrategy());  
  19.     }  
  20.     public Strategy creator(Integer type){  
  21.        return strategyMap.get(type);  
  22.     }  
  23.     public static StrategyFactory getInstance(){  
  24.        return factory;  
  25.     }  
  26. }   
客户端类:

[java]  view plain  copy
  1. package strategy.strategy;  
  2.    
  3. import strategy.RechargeTypeEnum;  
  4.    
  5. public class Client {  
  6.    
  7.      
  8.     public static void main(String[] args) {  
  9.    
  10.        Context context = new Context();  
  11.        // 网银充值100 需要付多少  
  12.        Double money = context.calRecharge(100D,  
  13.               RechargeTypeEnum.E_BANK.value());  
  14.        System.out.println(money);  
  15.    
  16.        // 商户账户充值100 需要付多少  
  17.        Double money2 = context.calRecharge(100D,  
  18.               RechargeTypeEnum.BUSI_ACCOUNTS.value());  
  19.        System.out.println(money2);  
  20.    
  21.        // 手机充值100 需要付多少  
  22.        Double money3 = context.calRecharge(100D,  
  23.               RechargeTypeEnum.MOBILE.value());  
  24.        System.out.println(money3);  
  25.    
  26.        // 充值卡充值100 需要付多少  
  27.        Double money4 = context.calRecharge(100D,  
  28.               RechargeTypeEnum.CARD_RECHARGE.value());  
  29.        System.out.println(money4);  
  30.     }  
  31.    
  32. }  


运行结果:

85.0

90.0

100.0

101.0


从上面代码可以看出,策略模式把具体的算法封装到了具体策略角色内部,增强了可扩展性,隐蔽了实现细 节;它替代继承来实现,避免了 if- else 这种不易维护的条件语句。当然我们也可以看到,策略模式由于独立策略实现,使得系统内增加了很多策略类;对客户端来说必须知道兜友哪些具体策略, 而且需要知道选择具体策略的条件。



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值