当前位置:   article > 正文

Java 开发之替换掉冗余的 if else 嵌套_java 使用map替换if else 调接口

java 使用map替换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
        }
     }
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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

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
	  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

多态实现:

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();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

好了,一个 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();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/377892
推荐阅读
相关标签
  

闽ICP备14008679号