2012-07-28 112 views
4

我有一种感觉,这个if/else应该被重构出来,但我不确定我能做什么,或者我是否应该让它如此...如何重构这个方法有多个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; 
} 

基本上我有一个报告摘要页面,列出三到四个报告。首先,如果条件是用户想要返回到该页面,则第二个条件是用户选择此特定报告时的条件;第三个条件是用户选择此报告作为独立报告(而非摘要页面) 。

+0

让它是因为它是。它很漂亮,因为它很简单。 – Nishant 2012-07-28 15:07:12

+0

我在代码中看不到任何不必要的东西,它非常清楚它的功能。保持原状。 – Keppil 2012-07-28 15:08:14

+0

保持原样,或使用Sprint MVC或JSF,它可以让你定义导航规则 – 2012-07-28 15:09:21

回答

5

如果您确实想改变它,你可以初始化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; 
} 

但实际上,它的罚款为是。

4

这种“基于守卫”的风格如何?它通常使得该方法从上到下更容易阅读。

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"; 
} 
+2

+1我认为这是最好的方法 - 它很明显,并消除了不必要的局部变量。 – mikera 2012-07-28 15:23:02

6

首先看看Design Pattern Command。它应该重构if/else的责任性,使其更有组织,更易于维护。然后你的代码应该是这样的:

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可以聚集在命令模式的更多知识

0

你的代码是罚款只是事情是这样的。 但是你也可以看看使用?:操作符,如果你想在一行中实现相同的操作。

一个例子是:

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); 
} 
} 

在结束您的代码会是这个样子:

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

这种风格很难阅读和维护。 – 2012-07-28 15:59:10

+0

正确!但如果@Mike想要消除其他条件,我没有看到其他选择。 – afrin216 2012-07-28 16:00:48