Java 开发之替换掉冗余的 if else 嵌套

替换掉 多层 if else 嵌套方法:

模拟场景:不同的渠道类型分别使用不同的逻辑处理。

渠道类型:储蓄卡、信用卡、公众号

一般实现:

 public static final String depositCard = "depositCard";
 public static final String creditCard = "creditCard ";
 public static final String OfficialAccounts = "OfficialAccounts";

 public void auto(String channelType) {
    if (!StringUtils.isEmpty(channelType)) {
        if (depositCard.equals(channelType)) {
            //TODO
        } else if (creditCard .equals(channelType)) {
            //TODO
        } else if (OfficialAccounts.equals(channelType)) {
            //TODO
        } else {
            //TODO
        }
     }
  }

接口分层:
接口分层,就是把接口分为对内接口和对外接口,非空的判断放在外层接口处理,对内接口传入的变量由外部接口保证不为空,从二减少空值判断。

public static final String depositCard = "depositCard";
public static final String creditCard = "creditCard ";
public static final String OfficialAccounts = "OfficialAccounts";
 
public void auto(String channelType) {
    if (!StringUtils.isEmpty(channelType)) {
        autoImpl(channelType);
    }
}

private void autoImpl(String channelType) {
	  if (depositCard.equals(channelType)) {
	      //TODO
	  } else if (creditCard .equals(channelType)) {
	      //TODO
	  } else if (OfficialAccounts.equals(channelType)) {
	      //TODO
	  } else {
	      //TODO
	  }
}

多态实现:

public abstract class Channel {
    public String type;
    public Channel(String type) {
        this.type = type;
    }
    public abstract void auto();
}

public class DepositCardChannel extends Channel {
    public DepositCardChannel(String type) {
        super(type);
    }
    @Override
    public void auto() {
        System.out.println("This is DepositCardChannel");
    }
}

public class CreditCardChannel extends Channel {
    public CreditCardChannel(String type) {
        super(type);
    }
    @Override
    public void auto() {
        System.out.println("This is CreditCardChannel");
    }
}

public void auto(Policy policy) {
    if (policy != null) {
        autoImpl(policy);
    }
}
public void autoImpl(Policy policy) {
    policy.auto();
}

好了,一个 if else 也没有了,如果有 新增类型,只需要增加一个 类继承 Channel,

使用Map替代分支语句:

private static Map<String, Class<? extends Policy>> map         = new HashMap<>();

public static final String   OPEN_POLICY = "OpenPolicy";
public static final String    SUB_POLICY  = "SubPolicy";

static {
    map.put(OPEN_POLICY, OpenPolicy.class);
    map.put(SUB_POLICY, SubPolicy.class);
}

public void auto(String policyType) {
    try {
        Class<? extends Policy> clazz = map.get(policyType);
        clazz.newInstance().auto();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值