赞
踩
大家在开发过程中,经常会用到if...else语句,尤其针对那些业务分支较多的情况下,if...else显得非常臃肿,大大影响代码的可读性,并且增加了维护的成本,一些大公司代码规范比较严格,单个类或者单个方法也有圈复杂度的最大限制。那么,针对以上问题,来和大家探讨下if...else的替代方案。假设我们现在要做一个计算器,实现加减乘除的功能:
- public static int calculate(String operator, int a, int b) {
- if ("add".equals(operator)) {
- return a + b;
- } else if ("subtract".equals(operator)) {
- return a - b;
- } else if ("multiply".equals(operator)) {
- return a * b;
- } else if ("divide".equals(operator)) {
- return a / b;
- }
- return 0;
- }
- public static int calculate(String operator, int a, int b) {
- switch (operator) {
- case "add":
- return a + b;
- case "subtract":
- return a - b;
- case "multiply":
- return a * b;
- case "divide":
- return a / b;
- default:
- return 0;
- }
- }
- public static int calculate(String operator, int a, int b) {
- return Calculator.calculate(operator, a, b);
- }
-
- public enum Calculator {
-
- ADD {
- @Override
- public int apply(int a, int b) {
- return a + b;
- }
- },
- SUBTRACT {
- @Override
- public int apply(int a, int b) {
- return a - b;
- }
- };
-
- abstract int apply(int a, int b);
-
- public static int calculate(String operatorName, int a, int b) {
- for (Calculator calculator : Calculator.values()) {
- if (calculator.toString().equalsIgnoreCase(operatorName)) {
- return calculator.apply(a, b);
- }
- }
- return 0;
- }
- }

- public interface Operation {
- int apply(int a, int b);
- }
-
- public class Add implements Operation {
- @Override
- public int apply(int a, int b) {
- return a + b;
- }
- }
-
- public class Subtract implements Operation {
- @Override
- public int apply(int a, int b) {
- return a - b;
- }
- }
-
- public class Calculator {
- private static Map<String, Operation> calcutorMap = new HashMap<>();
-
- static {
- calcutorMap.put("add", new Add());
- calcutorMap.put("subtract", new Subtract());
- }
-
- public static int calculate(String operator, int a, int b) {
- return calcutorMap.get(operator).apply(a, b);
- }
- }

- @FunctionalInterface
- public interface VoidFunction {
-
- void apply();
-
- }
- public class IfElse {
-
- private Map<Boolean, VoidFunction> logicAndFunctionMaps = new LinkedHashMap<>();
-
- public static IfElse create() {
- return new IfElse();
- }
-
- public IfElse ifLogic(boolean condition, VoidFunction ifFunction) {
- return addLogic(condition, ifFunction);
- }
-
- public IfElse elseIfLogic(boolean condition, VoidFunction ifFunction) {
- return addLogic(condition, ifFunction);
- }
-
- public IfElse elseLogic(VoidFunction ifFunction) {
- return addLogic(true, ifFunction);
- }
-
- private IfElse addLogic(boolean condition, VoidFunction ifFunction) {
- this.logicAndFunctionMaps.put(condition, ifFunction);
- return this;
- }
-
- public boolean execute() {
- for (Map.Entry<Boolean, VoidFunction> entry : logicAndFunctionMaps.entrySet()) {
- if (entry.getKey()) {
- entry.getValue().apply();
- return true;
- }
- }
- return false;
- }
-
- }

- public static void calculate(String payType) {
- IfElse.create()
- .ifLogic("alipay".equals(payType), () -> System.out.println("支付宝支付"))
- .elseIfLogic("wechat".equals(payType), () -> System.out.println("微信支付"))
- .elseLogic(() -> System.out.println("无效的支付方式"))
- .execute();
- }
综上所述,替代if...else的方案有很多种(包括策略模式、命令模式等等,偷个懒,这里就不一一列举了),有的是轻量级,有的则稍显笨重些,根据不同的业务场景选择合适的方案。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。