2013-03-16 58 views
-1

我在嵌套,如果我们的核心Java应用程序中的条件面临巨大的问题 代码摘要如下....如果条件 您可以有近20嵌套你能告诉我如何优化一段代码?嵌套,如果条件

如果条件可能有嵌套条件,并且从Java应用程序的设计角度来看可能是一个巨大的问题,那么避免嵌套的更好方法是什么?

请在Java的Java版本1.6

String condition = getCondition(); 
if (condition.equals(add)) { // add operation 
    add(); 
    if (condition.equals(sub)) {// sub operation 
     sub(); 
     if (condition.equals(div)) { //div operation 
      div(); 
      if (condition.equals(cos)) { // cos operation 
       cos(); 
      } 
     } 
    } 
} 

编辑解决方案帮助:我可以有更多的数学运算,比如说20多了,就会切换工作then.20操作是一个巨大的很多。

回答

1

状态模式:

public enum Operation { 
ADD { 
    int execute(int a, int b) { 
     return a + b; 
    } 
}, 
SUB { 
    int execute(int a, int b) { 
     return a - b; 
    } 
}, 
MUL { 
    @Override 
    int execute(int a, int b) { 
     return a * b; 
    } 
}, 
DIV { 
    @Override 
    int execute(int a, int b) { 
     return a/b; 
    } 
}; 

abstract int execute(int a, int b); 

public static void main(String[] args) { 
    Operation oper = getOperation(); 
    oper.execute(3, 4); 
} 

private static Operation getOperation() { 
    return Operation.MUL; 
} 

}

这样的:

public static void main(String[] args) { 
    String operation = user set it 
    Operation oper = getOperation(operation); 
    oper.execute(3, 4); 
} 

private static Operation getOperation(String operation) { 
    return Operation.valueOf(operation.toUpperCase()); 
} 

要小心该Operation.valueOf可能行NullPointerException如果操作为空或IllegalArgumentException如果操作不是其中之一操作枚举

1

改为使用switch声明。当你有很多决定时使用这个。

请注意,只有在想要在switch语句中使用String时,才能在JDK 7中发生此情况。在旧版本enum可能会有所帮助。

+0

很遗憾,我们不知道涉及的类型或OP使用的Java版本。这可能也可能不合适 - 在你的答案中值得明确的是这一点。 – 2013-03-16 09:39:45

+0

@ JonSkeet,使用的版本是java 1.6 – Deepak 2013-03-16 09:41:18

0

根据你的代码,它应该总是满足condition.equals(add)来执行下一行。根据网络中的条件,它永远不会满足下一个条件。当它进入下一行代码时。

要使用的字符串条件数检查

您可以使用switch

String condition = getCondition();  
    switch(condition) { 
     case add: 
      add(); 
      break; 
     case sub: 
      sub(); 
      break; 
     // etc... 
    } 

边注:打开可从Java7字符串。

2

的if else-if条件,而不是像这样你应该使用:

String condition = getCondition();  
if(condition.equals(add)) 
    add(); 
else if(condition.equals(sub)) 
    sub(); 
else if(condition.equals(div)) 
    div(); 
else if(condition.equals(cos)) 
    cos(); 
1

如果if语句不需要嵌套,则可以使用命令模式。

首先,在匹配器和命令之间建立一个映射。这些命令遵循一个通用的调用接口,如Runnable,Callable或如我的示例Command。该示例演示如何动态创建包装并使用静态或非静态类。如果实际的命令在手之前是未知的,那么这种模式很实用,因为稍后可能添加和删除命令。

public class CommandExample { 

    private interface Command { 
     public void execute(); 
    } 

    private Map<String, Command> commands = new HashMap<>(); 

    private void setUp() { 
     commands.put("add", new Command() { 
      public void execute() { 
       add(); 
      } 
     }); 
     commands.put("sub", new Sub()); 
     commands.put("arg", new Argument("the argument")); 
    } 

    private void add() { 
     System.out.println("Add called"); 
    } 

    private static class Sub implements Command { 
     @Override 
     public void execute() { 
      System.out.println("Sub called"); 
     } 
    } 

    private class Argument implements Command { 

     private final String arg; 

     public Argument(String arg) { 
      this.arg = arg; 
     } 

     @Override 
     public void execute() { 
      System.out.println("Argument called with arg " + arg 
        + " and access to outer class " + CommandExample.this); 
     } 
    } 

    private void execute(String... names) { 
     for (String name : names) { 
      Command command = commands.get(name); 
      if (command != null) { 
       command.execute(); 
      } else { 
       System.err.println("Command '" + name 
         + "' is not known. Only know " + commands.keySet()); 
      } 
     } 
    } 

    public static void main(String[] args) { 
     CommandExample commandExample = new CommandExample(); 
     commandExample.setUp(); 
     commandExample.execute("add", "sub", "arg", "unknown"); 
    } 
} 
+0

,你能给出一个工作的例子,以便我可以在我的系统中运行,或者你可以在www.ideone.com后面的java部分 – Deepak 2013-03-16 10:50:46

+0

@Deepak我认为代码适用于你给出的例子(你的方法没有参数)。但是,我会尝试做一个完整的例子,而不完全知道你需要什么。 – 2013-03-16 11:54:12

+0

@ Roger.but在这里我们不添加数字。你只需调用add(),sub()方法。 – Deepak 2013-03-16 15:25:23

1

在这里你有你如何使用枚举的例子。首先创建您的枚举

enum MathOperations{ 
    ADD, SUB, DIV, COS; 
} 

然后你可以使用它像这样

MathOperations m = MathOperations.valueOf(getCondition().toUpperCase); 
switch(m) { 
    case ADD: add(); break; 
    case SUB: sub(); break; 
    //and so on... 
} 

当然,如果getCondition()将返回元素是MathOperations它只会工作。否则,您将获得IllegalArgumentException


您也可以尝试使用Strategy pattern

1

您可以将add,sub,div, cos ...放入有序列表/数组中。然后使用for循环迭代列表。使用break运营商和reflection调用适当的方法。

final String[] OPERATION_LIST = { "add", "sub", "div", "cos" }; 
String condition = getCondition(); 
for (String op : OPERATION_LIST) { 
    if (condition.equals(op)) 
     getClass().getMethod(op).invoke(this); 
    else 
     break; 
} 

以上for循环等于您的嵌套if语句。其缺点是其他数学方法必须是public。如果没有,你需要像Accessing Private Methods

注意:如果你正在制作一个计算器(对吗?),也许Reverse Polish notation更好。