【问题标题】:How can I refactor that code ? (state pattern ?)如何重构该代码? (状态模式?)
【发布时间】:2010-05-19 10:34:24
【问题描述】:

如何重构该代码?

public enum enum1
{
    value1 = 0x01,
    value2 = 0x02,
    value3 = 0x03,
    value4 = 0x04,
    value5 = 0x05,
    UNKNOWN = 0xFF
}

class class1
{
    private const string STR_VALUE1 = "some text description of value1";
    private const string STR_VALUE2 = "some text description of value2";
    private const string STR_VALUE3 = "some text description of value3";
    private const string STR_VALUE4 = "some text description of value4";
    private const string STR_VALUE5 = "some text description of value5";
    private const string STR_VALUE6 = "some text description of unknown type";

    public static string GetStringByTypeCode(enum1 type)
        {
            switch(type)
            {
                case enum1.value1:
                    return STR_VALUE1;
                case enum1.value2:
                    return STR_VALUE2;
                case enum1.value3:
                    return STR_VALUE3;
                case enum1.value4:
                    return STR_VALUE4;
                case enum1.value5:
                    return STR_VALUE5;
                default:
                    return STR_VALUE6;
            }
        }
}

PS:有很多 enum1...enumX 和 GetStringByTypeCode(enum1) ... GetStringByTypeCode(enumX) 方法。

编辑:我是这样重构的:

namespace ConsoleApplication4
{
    public enum enum1
    {
        value1 = 0x01,
        value2 = 0x02,
        value3 = 0x03,
        value4 = 0x04,
        value5 = 0x05,
        UNKNOWN = 0xFF
    }

    class class1
    {
        static Dictionary<enum1, string> _dict;

        static class1()
        {
            _dict = new Dictionary<enum1, string>();

            _dict.Add(enum1.value1, "some text description of value1");
            _dict.Add(enum1.value2, "some text description of value2");
            _dict.Add(enum1.value3, "some text description of value3");
            _dict.Add(enum1.value4, "some text description of value4");
            _dict.Add(enum1.value5, "some text description of value5");
            _dict.Add(enum1.UNKNOWN, "some text description of unknown type");
        }

        public static string GetStringByTypeCode(enum1 type)
        {
            string result = string.Empty;

            try
            {
                _dict.TryGetValue(type, out result);
            }
            catch
            { 
            }

            return result;
        }
    } 


    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(class1.GetStringByTypeCode(enum1.value4));

            Console.ReadKey();
        }
    }
}

【问题讨论】:

  • 你想达到什么目的?代码少?能够更轻松地添加额外的价值/描述对吗?还有什么?
  • @Daniel,我认为该代码并不理想。也许有人可以提出一个更好的解决方案,所以我问一个问题......能够更轻松地添加额外的价值/描述对 --> 是的。

标签: c# refactoring


【解决方案1】:

您始终可以将枚举映射到Dictionary&lt;enum1, string&gt; 中的字符串,然后通过根据枚举键在字典中查找正确的字符串来实现您的方法。

【讨论】:

    【解决方案2】:

    另一个角度可能是使用DescriptionAttribute 并创建一个帮助方法来为您检索描述。这是一些额外的工作,但允许您将描述直接映射到枚举值,而无需维护列表,例如:

    public enum Enum1
    {
        [Description("This is value 1")]
        value1 = 0x001,
        [Description("This is value 2")]
        value2 = 0x002,
        [Description("This is value 3")]
        value3 = 0x003
    }
    
    ....
    
    public static class EnumExtensions
    {
        public static string GetDescription(this Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());
            DescriptionAttribute[] attributes =
                (DescriptionAttribute[])fi.GetCustomAttributes(
                 typeof(DescriptionAttribute), false);
            return (attributes.Length > 0) ? attributes[0].Description : value.ToString();
        }
    }
    

    然后,当您需要枚举的描述时,您只需这样做:

    Enum1 value = Emum1.Value1;
    string valueDesc = value.GetDescription();
    

    【讨论】:

      【解决方案3】:

      您可以重构为Dictionary&lt;int,string&gt;

      如果您想保留enum 及其含义,请使用Dictionary&lt;enum1,string&gt;

      // private field
      private Dictionary<enum1,string> myDictionary = new Dictionary<enum1,string>();
      
      // in constructor / other method
      myDictionary.Add(enum1.value1, "some text description of value1");
      myDictionary.Add(enum1.value2, "some text description of value2");
      myDictionary.Add(enum1.value3, "some text description of value3");
      myDictionary.Add(enum1.value4, "some text description of value4");
      myDictionary.Add(enum1.value5, "some text description of value5");
      myDictionary.Add(enum1.UNKNOWN, "some text description of unknown type");
      

      然后实现一个简单的查找:

      public string GetStringByTypeCode(enum1 type)
      {
          return myDictionary[type];
      }
      

      【讨论】:

        【解决方案4】:

        使用您需要的数据和操作创建一个状态类。让你的州继承它。

        abstract class State 
        {
           public string Description { get; set; }
           public void Behaviour();
        }
        

        你可以在它们的构造函数中初始化你的状态描述。

            public class MyClass
            {
               State s;
        
               public MyClass(enum1 type) 
               {  
                        switch(type)
                        {
                            case enum1.value1:
                                s = State1();
                                break;
                            case enum1.value2:
                                s = State2();
                                break;
                                ...
                        }
        
               }
            }
        

        您还可以使用 Map 将枚举映射到状态对象。 (Dictionary&lt;enum1, State&gt;)

        如果您在状态对象而不是不同的数据中有不同的行为,则状态模式会更有意义。因此,如果您想将 Enum 映射到 String,您可以使用刚才提到的字典。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-01-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-28
          • 2012-07-11
          相关资源
          最近更新 更多