c# - 您如何处理用户偏好?

标签 c# user-preferences

与大多数软件一样,用户可以指定他们希望如何处理某些事情。在我的例子中,用户可以指定他们喜欢哪种格式。有 3 个选项,保留未格式化、驼峰大小写或适当的大小写。我目前正在使用它,但感觉非常笨重和重复。这是类(class)的一员。

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,返回的格式化程序实例将取决于首选项。

这是一个非常简单的例子,只是为了用一些代码来阐明我的答案。这不是特别花哨,可能会使用更复杂的模式,但希望它是正确的起点。

定义接口(interface)和一些格式化程序

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

关于c# - 您如何处理用户偏好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3692279/

相关文章:

c# - 在 C#/VB 中使用 Windows API 结构的 Ansi 和 Unicode 版本的简单方法

c# - 有更好的设计选择吗?

c# - 如何使用通配符删除字符串的特定部分?

Cocoa 在一个 mac 帐户上有多个用户首选项?

c# - 检查何时使用 C# 上传 XML 文件

c# - 通过将参数视为文本来设置正则表达式模式

ios - 设置 bundle - 为 slider 项目指定标题

java - 在 Java/SWT 中保存窗口状态

webstorm - 重置 WebStorm 中的默认设置