策略模式+工厂模式(反射)+枚举代替 if..else if..

实际项目中我们经常碰到需要使用if..elseif的分支判断这种情况。这种写法带来一些弊端,第一,一旦分支多太多,逻辑复杂,会导致代码十分冗长,增加阅读难度;第二,违背了开闭原则。如果需要增加或减少分支,需要改动if…elseif,增大因代码改动而出错的风险。这种粗鄙,丑陋的代码是我们不能接受的。

if…else if…

if(payStrategyParam == "ALIPAY") {
    //...a bunch of code here
    System.out.println("pay with alipay: " + 12);
}else
if(payStrategyParam == "WECHATPAY") {
    //...a bunch of code here
    System.out.println("pay with wechatpay: " + 12);
}else
if(payStrategyParam == "EBANKPAY") {
    //...a bunch of code here
    System.out.println("pay with ebankpay: " + 12);
}

如标题,解决的方法是,我们可以通过策略模式+工厂模式,巧妙地利用反射机制和枚举类的valueof方法,规避掉if…elseif写法带来的弊端。下面我们通过一个付款这个实际例子来理解这种做法。

付款策略接口

public interface PayStrategy {

    void pay(double total);

}

支付宝付款

public class Alipay implements PayStrategy {

    public void pay(double total) {
        System.out.println("pay with alipay: " + total);
    }

}

微信付款

public class WechatPay implements PayStrategy {

    public void pay(double total) {
        System.out.println("pay with wechatpay: " + total);
    }

}

银行卡付款

public class EbankPay implements PayStrategy {

    public void pay(double total) {
        System.out.println("pay with ebankpay: " + total);
    }

}

付款枚举类

如果需要增删付款类型,只需要在PayEnum类添加或删除对应的枚举,而不是增删if…else if…

public enum PayEnum {

    ALIPAY("com.business.pay.Alipay"),
    WECHATPAY("com.business.pay.WechatPay"),
    EBANKPAY("com.business.pay.EbankPay");

    PayEnum(String className) {
        this.setClassName(className);
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    private String className;

}

工厂类

该工程类提供静态方法,巧妙地利用反射机制和枚举类的valueof方法,返回具体策略实例。

public class StrategyFactory {

    public static PayStrategy getStrategy(String strategyType) throws Exception {
        String className = PayEnum.valueOf(strategyType).getClassName();
        return (PayStrategy) Class.forName(className).newInstance();
    }

}

客户端测试

public class Client {

    public static void main(String[] args) throws Exception {

        String payStrategyParam = "ALIPAY";//Param from front end

        PayStrategy strategy = StrategyFactory.getStrategy(payStrategyParam);
        strategy.pay(12);

        payStrategyParam = "WECHATPAY";//Param from front end

        strategy = StrategyFactory.getStrategy(payStrategyParam);
        strategy.pay(12);

        payStrategyParam = "EBANKPAY";//Param from front end

        strategy = StrategyFactory.getStrategy(payStrategyParam);
        strategy.pay(12);
    }

}

测试结果

pay with alipay: 12.0
pay with wechatpay: 12.0
pay with ebankpay: 12.0

总结

if…else if…的缺点

  1. 多分支和复杂度高的逻辑会使代码冗长,难以理解和维护。
  2. 违反开闭原则,增删分支需要改动if…else if结构,,增大代码出错风险

使用策略模式+工厂模式的优点

  1. 对应的分支处理分成不同策略类来实现,使代码易于读懂和维护
  2. 扩展性好,增加分支只需要增加对应的策略实现类和枚举。符合开闭原则

猜你喜欢

转载自blog.csdn.net/u012557814/article/details/81671928