2010-01-25 73 views

回答

46

某些操作,這些操作是共同的幾個類可以被移動到輔助類,然後將其經由對象組合物中使用:

public class OrderService { 
    private PriceHelper priceHelper = new PriceHelper(); 

    public double calculateOrderPrice(order) { 
     double price = 0; 
     for (Item item : order.getItems()) { 
      double += priceHelper.calculatePrice(item.getProduct()); 
     } 
    } 
} 

public class ProductService { 
    private PriceHelper priceHelper = new PriceHelper(); 

    public double getProductPrice(Product product) { 
     return priceHelper.calculatePrice(product); 
    } 
} 

使用輔助類可以以多種方式來完成:

  • 直接實例化它們(如上述)通過依賴注入
  • 通過使他們的方法static和訪問噸以靜態方式下襬,如IOUtils.closeQuietly(inputStream)關閉InputStream,但沒有拋出異常。
  • 至少我的習慣是在命名類,只有靜態方法,而不是依賴XUtils,並classees這反過來又依賴/需要通過一個DI容器管理XHelper

(上面的例子只是一個樣本 - 它不應該在域驅動設計方面進行討論)

+2

所以意思是與實用工具類同義嗎?我經常使用「'blahUtil.java'」,但不斷看到類名爲''blahHelper.java'「 – wired00 2012-06-05 05:50:16

+7

的類關閉,但不完全。至少在我的慣例中,實用程序類是沒有任何依賴關係的靜態方法,而幫助程序具有依賴關係,因此具有非靜態方法。 – Bozho 2012-06-05 08:01:38

+0

對,我得到你,我想這只是歸結爲開發人員保持一種標準的做事方式,但我明白你的意思。關於Util是靜態的,歡呼聲 – wired00 2012-06-05 08:29:32

8

這些是「坐在一邊」的代碼主體,併爲對象做一些工作。他們「幫助」對象來完成它的工作。

作爲一個例子,很多人都有一個更接近輔助對象。這將採用各種可關閉的對象,例如java.sql.Statement,java.sql.Connection等,並將關閉對象,並忽略任何可能出現的錯誤。這往往是因爲如果你關閉一個對象時出現錯誤,無論如何你沒有辦法做到這一點,所以人們會忽略它。

不是有這個樣板:

try { 
    connection.close(); 
} catch (SQLException e) { 
    // just ignore… what can you do when you can't close the connection? 
    log.warn("couldn't close connection", e); 
} 

分散各地的代碼庫,他們只需撥打:

Closer.close(connection); 

代替。例如,看看番石榴closeQuietly

+1

嗯,至少你應該記錄異常,所以當你的連接池溢出或你的數據庫監聽器不給你新的連接,你會知道是什麼導致它。 – 2013-11-14 16:12:16

1

'幫助'方法通常是一種方法,使更容易,無論它是什麼。有時候,他們已經習慣了讓事情變得更可讀/可清楚地組織(有些人可能會爭辯說這一點,但它最終很主觀的):

public void doStuff() { 
    wakeUp(); 
    drinkCoffee(); 
    drive(); 
    work(); 
    goHome(); 
} 

在哪裏,自己的每一個「輔助方法,」是相當複雜的...概念變得非常清晰和簡單。

輔助方法的另一個非常好的用法是提供跨許多不同類的通用功能。最好的例子是Math類,它包含了大量的靜態幫助方法,可以幫助您計算諸如數字的日誌,數字的指數等。

您在哪裏畫線什麼是輔助方法,什麼是常規方法是非常主觀的,但這是它的要點。其他答案也很不錯。

0

在我看來,Helper類與在C++類中聲明的正常函數類似。例如,如果您需要多個類的全局常量,則可以定義一個包含最終靜態const變量的輔助類。

0
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.Scanner; 

public class Helpers { 
public static String getDate() { 
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); 
     return dateFormat.format(new Date()); 
    } 

    public static boolean isTimeABeforeTimeB(String timeA, String timeB) { 
     try { 
      SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm aa"); 
      Date dA = dateFormat.parse(timeA); 
      Date dB = dateFormat.parse(timeB); 
      if (dA.getTime() < dB.getTime()) { 
       return true; 
      } else { 
       return false; 
      } 
     } catch (Exception e) { 
      // 
     } 
     return false; 
    } 

    public static String getDateAndTimeInput(String prompt) { 
     Scanner input = new Scanner(System.in); 
     String ans; 
     SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm aa"); 
     dateFormat.setLenient(false); 
     boolean dateValid; 
     do { 
      System.out.print(prompt); 
      ans = input.nextLine(); 
      ans = ans.trim(); 
      dateValid = true; 
      try { 
       Date d = dateFormat.parse(ans); 
      } catch (Exception e) { 
       dateValid = false; 
      } 
     } while (!dateValid); 
     return ans; 
    } 


    public static String getStringInput(String prompt) { 
     Scanner input = new Scanner(System.in); 
     String ans; 

     do { 
      System.out.print(prompt); 
      ans = input.nextLine(); 
      ans = ans.trim(); 
     } while (ans.length() == 0); 
     return ans; 
    } 

    public static double getDoubleInput(String prompt) { 
     Scanner input = new Scanner(System.in); 
     double ans = 0; 
     boolean inputValid; 
     do { 
      System.out.print(prompt); 
      String s = input.nextLine(); 
      //Convert string input to integer 
      try { 
       ans = Double.parseDouble(s); 
       inputValid = true; 
      } catch (Exception e) { 
       inputValid = false; 
      } 
     } while (!inputValid); 
     return ans; 
    } 

    public static int getIntegerInput(String prompt) { 
     Scanner input = new Scanner(System.in); 
     int ans = 0; 
     boolean inputValid; 
     do { 
      System.out.print(prompt); 
      String s = input.nextLine(); 
      // Convert string input to integer 
      try { 
       ans = Integer.parseInt(s); 
       inputValid = true; 
      } catch (Exception e) { 
       inputValid = false; 
      } 
     } while (!inputValid); 
     return ans; 
    } 

    public static int getIntegerInput(String prompt, int lowerBound, int upperBound) { 
     Scanner input = new Scanner(System.in); 
     int ans = 0; 
     boolean inputValid; 
     do { 
      System.out.print(prompt); 
      String s = input.nextLine(); 
      // Convert string input to integer 
      try { 
       ans = Integer.parseInt(s); 
       if (ans >= lowerBound && ans <= upperBound) { 
        inputValid = true; 
       } else { 
        inputValid = false; 
       } 
      } catch (Exception e) { 
       inputValid = false; 
      } 
     } while (!inputValid); 
     return ans; 
    } 
} 

這是一個助手類的例子。它包含了哪些方法是項目中其他類的常用方法。

例如,如果有人想從類中輸入整數,則必須輸入以下內容:String num = Helpers.getIntegerInput(「input your number」);

提示符是顯示給用戶的輸出。其他例子輸入一個字符串,雙精度,日期和時間等