JAVA探寻IF-ELSE的优化

一般在做逻辑判断的时候我们一般难免会写出以下的代码,以电商的会员制度为例

if (type == "AVIP") {
        System.out.println("A output");   
 } else if (type == "BVIP") {
        System.out.println("B output");     
 } else if (type == "CVIP") {
        System.out.println("C output");   
 } else {  
      System.out.println("output");     
}}
  1.  1对于以上的代码要优化的化很容易就想到了策略模式来做优化,首先定义一个策略接口
public interface StrategyInf {
    void comput(BigDecimal money);
    String getType();
}
  1. 2具体策略的实现
publi class CommonStrategy implements StrategyInf {
        @Override
        public void comput(BigDecimal money) {
            Optional.ofNullable(money).ifPresent(m -> {
                System.out.println(m.multiply(new BigDecimal(VIPEnums.COMMON_VIP.getSaleCount())));
            });
        }

        @Override
        public String getType() {
            return VIPEnums.COMMON_VIP.getType();
        }
    }
    public static class SilverStrategy implements StrategyInf {
        @Override
        public void comput(BigDecimal money) {
            Optional.ofNullable(money).ifPresent(m -> {
                System.out.println(m.multiply(new BigDecimal(VIPEnums.SLIVER_VIP.getSaleCount())));
            });
        }

        @Override
        public String getType() {
            return VIPEnums.SLIVER_VIP.getType();
        }
    }

    public class GoldStrategy implements StrategyInf {
        @Override
        public void comput(BigDecimal money) {
            Optional.ofNullable(money).ifPresent(m -> {
                System.out.println(m.multiply(new BigDecimal(VIPEnums.GOLD_VIP.getSaleCount())));
            });
        }

        @Override
        public String getType() {
            return VIPEnums.GOLD_VIP.getType();
        }
    }

    public class PlatinumStrategy implements StrategyInf {
        @Override
        public void comput(BigDecimal money) {
            Optional.ofNullable(money).ifPresent(m -> {
                System.out.println(m.multiply(new BigDecimal(VIPEnums.PLATINUM_VIP.getSaleCount())));
            });
        }

        @Override
        public String getType() {
            return VIPEnums.PLATINUM_VIP.getType();
        }
    }

这个时候发现最开始的代码就做了一层封装,变成了如下

if (type == AVIP) {
    new AStrategy().comput();
} else if (type == BVIP) {
    new BStrategy().comput();
} else if (type == CVIP){
    new CStrategy().comput();
} else {
    .....
}

以上的代码确实看起来好些了,但是进一步优化可以把IF-ELSE省略, 工厂+策略模式

    public static class StrategyFactory {
        private Map stratgyMap;
        private volatile static StrategyFactory INSTANCE = null;
        private StrategyFactory() {
            List strategies = new ArrayList<>();
            strategies.add(new CommonStrategy());
            strategies.add(new SilverStrategy());
            strategies.add(new GoldStrategy());
            strategies.add(new PlatinumStrategy());
            stratgyMap = strategies.stream()
                                    .collect(Collectors.toMap(StrategyInf::getType,strategy -> strategy));
        }
        public static StrategyFactory getInstance() {
            synchronized (StrategyFactory.class) {
                if (INSTANCE == null) {
                    INSTANCE = new StrategyFactory();
                }
            }
            return INSTANCE;
        }
        public StrategyInf getStrategy(String type) {
            return stratgyMap.get(type);
        }
    }

最终只要一行代码就能达到最开始的效果

public static void main(String[] args) {
    StrategyFactory.getInstance().get(type).comput();
}

 

你可能感兴趣的:(设计模式,java,spring)