簡體   English   中英

您如何處理用戶偏好?

[英]How do you handle user preferences?

像大多數軟件一樣,用戶可以指定他們希望如何處理某些事情。 就我而言,用戶可以指定自己喜歡的格式。 有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;
    }
}

該方法本身並不感到笨拙。 這就是它的調用方式。 每次我想要獲取格式化的文本時,總是必須通過用戶首選項似乎並不是最好的方法。 我做一個普通的類並通過構造函數傳遞應用程序首選項對象會更好嗎?

謝謝。

一種選擇是創建某種工廠類,然后可以使用包含首選項的類的實例或從其中實例化工廠類。

使用工廠類,您可以獲取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");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM