2010-09-11 76 views
7

與大多數軟件一樣,用戶可以指定他們想如何處理某些事情。就我而言,用戶可以指定他們喜歡什麼樣的格式。有3個選項,留下未格式化的,駱駝案件或適當的案件。我目前有它的工作,但感覺非常笨重和重複。這是班上的一位傑出人士。你如何處理用戶偏好?

public static class Extensions 
{ 
    public static string GetPreferenceFormattedText(this string text, ApplicationPreferences applicationPreferences, bool pluralize) 
    { 
     if (applicationPreferences.FieldNamingConvention.Equals(FieldNamingConvention.SameAsDatabase)) 
      return text; 
     string formattedText = text.Replace('_', ' '); 
     formattedText = formattedText.MakeTitleCase(); 
     formattedText = formattedText.Replace(" ", ""); 

     if (applicationPreferences.FieldNamingConvention.Equals(FieldNamingConvention.Prefixed)) 
      return applicationPreferences.Prefix + formattedText; 

     return applicationPreferences.FieldNamingConvention.Equals(FieldNamingConvention.CamelCase) 
        ? formattedText.MakeFirstCharLowerCase() 
        : formattedText; 
    } 
} 

該方法本身並不真正感到笨重。這就是它被調用的方式。每次我想要獲取格式化文本時,始終必須傳遞用戶首選項似乎不是最佳方式。我會做更好的常規課程,並通過構造函數傳遞應用程序首選項對象嗎?

謝謝。

回答

6

一種選擇是創建某種工廠類,然後可以使用包含首選項的類的實例或通過包含首選項的類的實例來實例化工廠類。

使用工廠類可以得到一個TextFormatter,返回的格式化程序的實例將取決於首選項。

這是一個非常簡單的例子,只是用一些代碼來闡明我的答案。這不是超級花式,可以使用更復雜的圖案,但希望這是正確的起點。

定義的接口和一些格式化

public interface IIdentifierFormatter 
    { 
    string FormatText(string text); 
    } 

    public class UnformattedIdenifierFormatter : IIdentifierFormatter 
    { 
    public string FormatText(string text) 
    { 
     return text; 
    } 
    } 

    public class CamelCaseIdenifierFormatter : IIdentifierFormatter 
    { 
    public string FormatText(string text) 
    { 
     // Camel case formatting here 
     return text; 
    } 
    } 

    public class ProperCaseIdenifierFormatter : IIdentifierFormatter 
    { 
    public string FormatText(string text) 
    { 
     // Proper case formatting here 
     return text; 
    } 
    } 

現在樣品偏好類

enum NamingConvention 
    { 
    Unformatted, 
    CamelCase, 
    ProperCase 
    } 

    public class Preferences 
    { 
    public NamingConvention FieldNamingConvention { get; set; } 
    // .. Other settings 


    // Function to get the formatter depending on the FieldNamingConvention 
    public IIdentifierFormatter GetFieldNameFormatter() 
    { 
     switch (FieldNamingConvention) 
     { 
     case NamingConvention.Unformatted: 
      return new ProperCaseIdenifierFormatter(); 
     case NamingConvention.CamelCase: 
      return new ProperCaseIdenifierFormatter(); 
     case NamingConvention.ProperCase: 
      return new ProperCaseIdenifierFormatter();   
     default: 
      throw new Exception("Invalid or unsupported field naming convention."); 
     }  
    } 
    } 

使用代碼

// Preferences loaded from some source, 
// for the example I just initialized it here.  
    Preferences pref = new Preferences(); 
    pref.FieldNamingConvention = NamingConvention.CamelCase; 

    // Get the formatter 
    IIdentifierFormatter formatter = pref.GetFieldNameFormatter(); 

    string formatted = formatter.FormatText("the_name_to_format"); 
+0

哇,謝謝。這很容易測試和管理。 – Mike 2010-09-11 21:05:59