2012-07-28 118 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