【问题标题】:How do you handle user preferences?您如何处理用户偏好?
【发布时间】:2011-04-11 04:10:31
【问题描述】:

与大多数软件一样,用户可以指定他们希望如何处理某些事情。就我而言,用户可以指定他们喜欢的格式。有 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;
    }
}

这个方法本身并不觉得笨重。这是它被称为的方式。每次我想获取格式化文本时总是必须传递用户偏好似乎不是最好的方法。制作一个常规类并通过构造函数传递应用程序首选项对象会更好吗?

谢谢。

【问题讨论】:

    标签: c# user-preferences


    【解决方案1】:

    一种选择是创建某种工厂类,然后您可以使用或从包含首选项的类的实例实例化工厂类。

    使用工厂类可以获得一个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");
    

    【讨论】:

      猜你喜欢
      • 2015-05-16
      • 2019-01-22
      • 1970-01-01
      • 2010-09-07
      • 1970-01-01
      • 1970-01-01
      • 2011-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多