开发者

How can I refactor that code ? (state pattern ?)

开发者 https://www.devze.com 2022-12-31 12:29 出处:网络
How can I refactor that code ? public enum enum1 { value1 = 0x01, value2 = 0x02, value3 = 0x03, value4 = 0x04,

How can I refactor that code ?

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: there are many enum1...enumX and GetStringByTypeCode(enum1) ... GetStringByTypeCode(enumX) methods.

EDIT: I refactored it this way:

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[] a开发者_如何转开发rgs)
        {
            Console.WriteLine(class1.GetStringByTypeCode(enum1.value4));

            Console.ReadKey();
        }
    }
}


You could always map the enum to strings in a Dictionary<enum1, string>, and then implement your method by looking up the correct string in the dictionary based on the enum key.


Another angle could be to use the DescriptionAttribute and create a helper method to retrieve the description for you. It is a little bit of extra work but will allow you to map the description directly to the enum value without having to maintain a list, something like:

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

Then when you need the description of your enum you would simply do:

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


You can refactor to a Dictionary<int,string>.

If you want to preserve the enum and its meaning, use Dictionary<enum1,string>.

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

Then implement a simple lookup:

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


Create a State-class with the data and operations you need. And let your states inherit from it.

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

You can initialize a the description of your states in their constructors.

    public class MyClass
    {
       State s;

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

       }
    }

You could also use a Map to map the enum to the state objects. (Dictionary<enum1, State>)

The state pattern makes more sense if you have different behaviours in your state objects rather than different data. So if you want to map an Enum to String you could use, as just mentioned, a dictionary.

0

精彩评论

暂无评论...
验证码 换一张
取 消