简体   繁体   English

如何重构这个有多个if / else语句的方法

[英]how to refactor this method which has multiple if/else statements

I have a feeling that this if/else should be refactored out but I'm unsure of what I can do, or whether I should just let it be as it is... 我有一种感觉,这个if / else应该被重构,但我不确定我能做什么,或者我是否应该让它像它一样......

private String someReportUrl(HttpServletRequest request, HttpServletResponse response) {
    String url;
    if (isBackToReportsSummary(request)) {
        url = SUMMARY_PAGE;
        getReportsSummary(request, response);
    } else if (isComingFromPageA(request)) {
        url = getTabUrl(request, REPORT_URL_FOR_PAGE_A);
    }
    else {
        url = "/standAlone/reportUrl.jsp";
    }
    return url;
}

Basically I have a reports summary page which lists three to four reports. 基本上我有一个报告摘要页面,其中列出了三到四份报告。 First if condition is when the user wants to go back to that page, second condition is for when user has selected this particular report, and the third condition is for when the user selects this report as a stand alone report (not from summary page). 首先,如果条件是用户想要返回该页面,则第二个条件是用户选择此特定报告时的情况,第三个条件是用户选择此报告作为独立报告(而不是摘要页面) 。

First take a look at the Design Pattern Command . 首先来看看Design Pattern Command It should refactor the if/else 's responsability, making it more organized and much more maintainable. 它应该重构if/else的责任,使其更有条理,更易于维护。 And then you code should look like this: 然后您的代码应如下所示:

Example

class ExampleServlet  {

  private HashMap commandMap = new HashMap();

  public ExampleServlet() {
    commandMap.put("create", new ActionTypeCreate());
    commandMap.put("replace", new ActionTypeReplace());
    commandMap.put("update", new ActionTypeUpdate());
    commandMap.put("delete", new ActionTypeDelete());
  } //endconstructor
} //endclass: ExampleServlet

private void performTask(String action) {
    ActionType cmd = (ActionType)commandMap.get(action);
    cmd.execute();
} //endmethod: performTask

HERE You can gather more knowledge in command pattern 在这里你可以收集更多的命令模式知识

If you absolutely want to change it, you could initialise url to the default return and only change it if one of the two conditions is met: 如果您绝对想要更改它,可以将url初始化为默认返回值,只有在满足以下两个条件之一时才更改它:

private String someReportUrl(HttpServletRequest request, HttpServletResponse response) {
    String url = "/standAlone/reportUrl.jsp";
    if (isBackToReportsSummary(request)) {
        url = SUMMARY_PAGE;
        getReportsSummary(request, response);
    } else if (isComingFromPageA(request)) {
        url = getTabUrl(request, REPORT_URL_FOR_PAGE_A);
    }
    return url;
}

But really, it's fine as is. 但实际上,它很好。

How about this "guard based" style? 这种“基于守卫”的风格怎么样? It often makes the method easier to read from top to bottom. 它通常使该方法从上到下更容易阅读。

private String someReportUrl(HttpServletRequest request, HttpServletResponse response) {
    if (isBackToReportsSummary(request)) {
        getReportsSummary(request, response);
        return SUMMARY_PAGE;
    } 
    if (isComingFromPageA(request)) {
        return getTabUrl(request, REPORT_URL_FOR_PAGE_A);
    }
    return "/standAlone/reportUrl.jsp";
}

You code is fine just the way it is. 你的代码就好了。 But you can also look into using ?: operator, if you want to achieve the same in a single line. 但是如果你想在一行中实现相同的功能,你也可以考虑使用?:operator。

An example would be: 一个例子是:

class round{
    public static void main(String args[]){

    int sampleInt=3;
    if(sampleInt==1){
        sampleInt = 5;
        System.out.println("One");
    }
    else if(sampleInt==2){
    sampleInt = 3;
        System.out.println("Two");
    }
    else{
        sampleInt = 4;
        System.out.println("Else");
    }

    sampleInt = sampleInt==1?5:(sampleInt==2?3:4);
    System.out.println("sampleInt "+sampleInt);
}
}

At the end your code would look something like this: 最后,您的代码看起来像这样:

   url = isBackToReportsSummary(request)==true?SUMMARY_PAGE:(isComingFromPageA(request)==true?getTabUrl(request, REPORT_URL_FOR_PAGE_A):"/standAlone/reportUrl.jsp");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM