你如何处理用户偏好?

7

像大多数软件一样,用户可以指定他们希望如何处理某些事情。在我的情况下,用户可以指定他们希望使用哪种格式。有三个选项:不进行格式化、驼峰命名法或首字母大写。目前我已经实现了这个功能,但它感觉非常笨重和重复。以下是该类的要点。

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;
    }
}

这个方法本身并不会让人感到笨拙。问题在于它的调用方式。每次想要获取格式化文本时都需要传递用户偏好设置,似乎不是最好的选择。我是否最好创建一个常规类,并通过构造函数传递应用程序偏好对象呢?

谢谢。

1个回答

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");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接