使用Map和枚举类型的组合优化多重if判断

private String getPositioningType(String customerType,String positioningType) {
        String type = "";
        if ("1".equals(customerType)) {
            switch (positioningType) {
                case "1":
                    type = "小米";
                    break;
                case "2":
                    type = "小红";
                    break;
                case "3":
                    type = "小李";
                    break;
            }
        } else if ("2".equals(customerType)) {
            switch (positioningType) {
                case "1":
                    type = "李老师";
                    break;
                case "2":
                    type = "王老师";
                    break;
                case "3":
                    type = "张老师";
                    break;
            }
        }
        return type;
    }

现在有这样一段代码,根据多个不同的类型匹配返回相应的值,如果类型越多,那代码中的if判断也会很长,显得不那么友好,所以对上面代码进行相应的优化; 

使用Map和枚举类型
public enum CustomerType {
    INDIVIDUAL("1") {
        @Override
        public String getTypeByCode(String code) {
            return INDIVIDUAL_TYPE_MAP.getOrDefault(code, "");
        }
    },
    ORGANIZATION("2") {
        @Override
        public String getTypeByCode(String code) {
            return ORGANIZATION_TYPE_MAP.getOrDefault(code, "");
        }
};

    //每种客户类型,使用Map来存储其可能的定位类型
    private static final Map<String, String> INDIVIDUAL_TYPE_MAP = new HashMap<String, String>() {{
        put("1", "小米"); 
        put("2", "小红");
        put("3", "小李");
    }};
    private static final Map<String, String> ORGANIZATION_TYPE_MAP = new HashMap<String, String>() {{
        put("1", "李老师");
        put("2", "王老师"); 
        put("3", "张老师");
    }};
    private final String code;

    CustomerType(String code) {
        this.code = code;
    }

    public static CustomerType getByCode(String code) {
        for (CustomerType customerType : values()) {
            if (customerType.code.equals(code)) {
                return customerType;
            }
        }
        return null;
    }

    public abstract String getTypeByCode(String code);
}

出入参数调用方法:

private static String getPositioningType(String customerType, String positioningType) {
        CustomerType type = CustomerType.getByCode(customerType);
        if (type != null) {
            return type.getTypeByCode(positioningType);
        }
        return "";
    }

        使用枚举类型来表示不同的客户类型,并为每个类型提供一个抽象方法getTypeByCode()。该方法在每个枚举常量中实现,以返回指定代码的定位类型。通过静态方法getByCode(),我们可以根据传入的代码获取对应的客户类型。如果找不到匹配的客户类型,则返回null。

        对于每种客户类型,我们都使用Map来存储其可能的定位类型。在getPositioningType()方法中,我们首先使用getByCode()方法获取相应的客户类型。如果找到了匹配的客户类型,则调用其getTypeByCode()方法获取对应的定位类型。如果找不到匹配的客户类型,则返回一个空字符串。

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值