質問

ほとんどのソフトウェアと同様に、ユーザーは特定のものをどのように処理したいかを指定できます。私の場合、ユーザーはどんな種類のフォーマットを好むかを指定できます。 3つのオプションがあり、フォーメットのないラクダのケースまたは適切なケースを残します。私は現在それを機能させていますが、とても不格好で反復的だと感じています。これがクラスのJistです。

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つのオプションは、ある種の工場クラスを作成することです。その後、設定を含むクラスのインスタンスで、またはそれ以外の工場クラスをインスタンスすることができます。

Factoryクラスを使用して、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帰属
所属していません StackOverflow
scroll top