利用工厂与策略模式优化超复杂业务分支逻辑

问题

       if (xxx条件) {
         A策略
       } else if (xxx条件) {
         B策略
       } else if (xxx条件) {
         C策略
       } ...

解决方案:

创建一个接口类:Strategy

public interface Strategy {

    // 条件是否符合,使用此策略
    boolean isSupport(Condition condition);

    // 策略的具体行为
    void exactStrategy();
}

创建一个条件类

public class Condition {
    // 简单的通过判断年龄,来判断满不满足条件,这里不是重点。
    private int age;


    public Condition(int age) {
        this.age = age;
    }

    public Condition() {

    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

一个具体的实现类,就是一个具体的策略,包含了使用条件,具体行为

public class StrategyImpl1 implements Strategy {
    @Override
    public boolean isSupport(Condition condition) {
        if (condition.getAge() < 18 && condition.getAge() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public void exactStrategy() {
        System.out.println("这是A策略。。。。。");
    }
}
public class StrategyImpl2 implements Strategy {
    @Override
    public boolean isSupport(Condition condition) {
        if (condition.getAge() >= 18 && condition.getAge() <= 50) {
            return true;
        }
        return false;
    }


    @Override
    public void exactStrategy() {
        System.out.println("这是B策略。。。。。");
    }
}
public class StrategyImpl3 implements Strategy {
    @Override
    public boolean isSupport(Condition condition) {
        if (condition.getAge() > 50) {
            return true;
        }
        return false;
    }

    @Override
    public void exactStrategy() {
        System.out.println("这是新增加的C策略,不需要改动原有的代码,只需要再添加一个 Strategy 的实现类就行了,然后添加到工厂里面");
    }
}

工厂类

public class Factory {
    private final static Factory factory = new Factory();

    private ArrayList<Strategy> strategyList = new ArrayList<>();

    private Factory() {
        // 这里可以通过其他的途径,从而避免修改代码:
        //                      配置文件
        //                      自定义注解扫描,自动加载这些实现类
        //                      ....
        strategyList.add(new StrategyImpl1());
        strategyList.add(new StrategyImpl2());
        // 新增的策略
        strategyList.add(new StrategyImpl3());
    }

    public static Factory getInstance() {
        return factory;
    }

    public Strategy getStrategy(Condition condition) {
        Strategy res = null;
        for (Strategy strategy : strategyList) {
            // 这里面业务逻辑,就简写了,不是重点
            if (strategy.isSupport(condition)) {
                res = strategy;
                break;
            }
        }
        if (res == null) {
            throw new RuntimeException("没有匹配的策略");
        }
        return res;
    }
}

测试代码

    public static void main(String[] args) {
//        if (xxx条件) {
//          A策略
//        } else if (xxx条件) {
//          B策略
//        } else if (xxx条件) {
//          C策略
//        }

        // 使用 工厂模式 + 策略模式,就可以避免大量if else if 的屎山代码
        Factory factory1 = Factory.getInstance();

        Strategy strategy2 = factory1.getStrategy(new Condition(3));
        strategy2.exactStrategy();

        Strategy strategy = factory1.getStrategy(new Condition(18));
        strategy.exactStrategy();

        Strategy strategy3 = factory1.getStrategy(new Condition(88));
        strategy3.exactStrategy();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一切随缘~~~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值