2009-04-10 51 views
17

過了一段時間,每個程序員都會得到一組實用程序類。其中一些是真正的編程珍珠,它們在你的幾個項目中被重複使用。例如,在Java:什麼是您最重用的課程?

class Separator { 

     private String separator; 
     private boolean called; 

     public Separator(String aSeparator) { 
      separator = aSeparator; 
      called = false; 
     } 

     @Override 
     public String toString() { 
      if (!called) { 
       called = true; 
       return ""; 
      } else { 
       return separator; 
      } 
     } 
    } 

和:

public class JoinHelper { 

    public static <T> String join(T... elements) { 
     return joinArray(" ", elements); 
    } 

    public static <T> String join(String separator, T... elements) { 
     return joinArray(separator, elements); 
    } 

    private static <T> String joinArray(String sep, T[] elements) { 
     StringBuilder stringBuilder = new StringBuilder(); 
     Separator separator = new Separator(sep); 

     for (T element : elements) { 
      stringBuilder.append(separator).append(element); 
     } 

     return stringBuilder.toString(); 
    } 
} 

什麼是最重用的類?

+2

製作這個社區wiki,請。否則,你有可能關閉。 – Randolpho 2009-04-10 16:02:07

+2

難道你不能只使用Apache的commons-lang中的StringUtils#join嗎? – 2009-06-04 09:25:11

+0

這只是一個可能的「homegrow」工具​​類的例子 – dfa 2009-06-04 14:07:42

回答

4

有記錄和電子郵件功能的實用程序類的工作人員有重複使用簡單的文件中大約2十幾個項目。包含擴展方法的擴展類。報告類,基本上利用報告服務Web服務,並可以很容易地流報告爲Excel,PDF等

例子...
1)工具類(靜態)

public static void LogError(Exception ex) 
    { 
     EventLog log = new EventLog(); 
     if (ex != null) 
     { 
      log.Source = ConfigurationManager.AppSettings["EventLog"].ToString(); 
      StringBuilder sErrorMessage = new StringBuilder(); 
      if (HttpContext.Current.Request != null && HttpContext.Current.Request.Url != null) 
      { 
       sErrorMessage.Append(HttpContext.Current.Request.Url.ToString() + System.Environment.NewLine); 
      } 
      sErrorMessage.Append(ex.ToString()); 
      log.WriteEntry(sErrorMessage.ToString(), EventLogEntryType.Error); 
     } 
    } 

2)擴展類

public static IEnumerable<TSource> WhereIf<TSource>(this IEnumerable<TSource> source, bool condition, Func<TSource, bool> predicate) 
    { 
     if (condition) 
      return source.Where(predicate); 
     else 
      return source; 
    } 
1

記錄器類:它將日誌文件中的控制流記錄下來。

1

配置讀卡器/二傳手:從INI/XML文件中讀取配置和設置應用程序

1

的環境中重複使用次數最多?嗯...

的boost :: shared_ptr的<>用的boost :: weak_ptr的<>

可能是最重用(也可能是最邦換降壓比)

1

全局

只是一個帶有靜態DBConnString的簡單類,以及一些其他應用程序範圍的設置。

因爲與.net

3

大多數重用,但無聊:

public static void handleException(Exception e) throws RuntimeException { 
    if (e instanceof RuntimeException) { 
     throw (RuntimeException) e; 
    } 

    throw new RuntimeException(e); //NOPMD 
} 

不那麼無聊(也方法的樓宇名單,並集):

/** 
    * Builds a Map that is based on the Bean List. 
    * 
    * @param items Bean List items 
    * @param keyField Bean Field that will be key of Map elements (not null) 
    * @return a Map that is based on the Bean List 
    */ 
    @SuppressWarnings("unchecked") 
    public static <T, K> Map<K, T> buildMapFromCollection(final Collection<T> items, 
                 boolean linkedMap, 
                 final String keyField, 
                 final Class<K> keyType) { 
    if (items == null) { 
     return Collections.emptyMap(); 
    } 

    if (keyField == null) { 
     throw new IllegalArgumentException("KeyField is null"); 
    } 

    final Map<K, T> result; 

    if (linkedMap) { 
     result = new LinkedHashMap<K, T>(); 
    } else { 
     result = new HashMap<K, T>(); 
    } 

    BeanMapper mapper = null; 
    for (final T item : items) { 
     if (mapper == null) { 
     mapper = new BeanMapper(item.getClass()); 
     } 
     final K key = (K) mapper.getFieldValue(item, keyField); 
     result.put(key, item); 
    } 
    return result; 
    } 
+1

你應該重新命名它doNotHandleException(...):) – 2011-01-25 16:55:27

1

一個ConcurrentDictionary我寫的,我現在似乎到處使用(我寫的很多並行程序)

3
public static short getLastDayOfMonth(short givenMonth, short givenYear) 
{ 
    short lastDay = 31; 
    switch (givenMonth) 
    { 
     case 4: 
     case 6: 
     case 9: 
     case 11: 
      lastDay = 30; 
      break; 
     case 2: 
      if ((int)givenYear % 4 == 0) 
      { 
       lastDay = 29; 
      } 
      else 
      { 
       lastDay = 28; 
      } 
      break;  
    } 
    return lastDay; 
} 
相關問題