switch语句是我们日常工作中最常见也是争论最多的(goto被忽视的前提下)。在重构中也把switch语句看成是一种代码的坏味道。但如何改造现有的switch代码并在今后去避免呢?本文从两方面进行探讨。
1 类型转化
    在不同的抽象层次上对于同一个对象可能会用不同的定义。举个简单的例子,在计算器中,用户输入的操作符号可能是字符型的,而程序内部实现的时候需要用枚举型。因此可能就会有这样的函数。
         public class Calculator
        {
                 public enum OPERATOR {Add, Minus, Multiply, Divide, Unknown};
                 public static OPERATOR GetOperator( char ch)
                {
                        OPERATOR op = OPERATOR.Unknown;
                         switch (ch)
                        {
                                 case '+':
                                        op = OPERATOR.Add;
                                         break;
                                 case '-':
                                        op = OPERATOR.Minus;
                                         break;
                                 case '*':
                                        op = OPERATOR.Multiply;
                                         break;
                                 case '/':
                                        op = OPERATOR.Divide;
                                         break;
                                 default:
                                         break;
                        }
                         return op;
                }
        }

   代码不长,不过写的过程中我却拷贝几次(case部分的代码)。原因就是代码的结构性重复还是比较严重的,而且如果以后支持其他新的操作时候,这代码不仅要改变,而且还会不断变冗长。认真想想,避免的办法还是有的,用Dictionary做一个映射表就可以实现。
 
     public class Calculator
        {
                 public enum OPERATOR { Add, Minus, Multiply, Divide, Unknown};
                 static private var OperatorMap = new Dictionary< char, OPERATOR> {
                        { '+', OPERATOR.Add },
                        { '-', OPERATOR.Minus },
                        { '*', OPERATOR.Divide },
                        { '/', OPERATOR.Multiply },
                };
                 public static OPERATOR GetOperator2( char ch)
                {
                         if (OperatorMap.ContainsKey(ch))
                                 return OperatorMap[ch];
                         else
                                 return OPERATOR.Unknown;
                }
        }
这样,不仅代码简洁了很多,而且从本质上已经从逻辑代码维护变成了映射表的维护。
2 动作调用
接续刚才的例子,这次不仅要实现操作符转化,而且要实现真正的运算。第一个反应又是想到了switch。实现如下:
 
     public class Calculator
        {
                 static public int Calculate( char op, int number1, int number2)
                {
                         int result = 0;
                         switch (GetOperator(op))
                        {
                                 case OPERATOR.Add:
                                        result = number1 + number2;
                                         break;
                                 case OPERATOR.Minus:
                                        result = number1 - number2;
                                         break;
                                 case OPERATOR.Multiply:
                                        result = number1 * number2;
                                         break;
                                 case OPERATOR.Divide:
                                        result = number1 / number2;
                                         break;
                                 default:
                                         throw new Exception( "Unsupported Operation!");
                        }
                         return result;
                }
        }
   跟之前遇到的问题有些类似,不过这回需要调用函数。通过.Net环境提供的Dictionary以及Func Delegate的解决如下:
         public class Calculator
        {
                 static private Dictionaryint, int, int>> calculationAction = new Dictionaryint, int, int>> {
                        { OPERATOR.Add, Add },
                        { OPERATOR.Minus, Minus },
                        { OPERATOR.Multiply, Multiply },
                        { OPERATOR.Divide, Divide }            
                };
                 public static OPERATOR GetOperator2( char ch)
                {
                         if (OperatorMap.ContainsKey(ch))
                                 return OperatorMap[ch];
                         else
                                 return OPERATOR.Unknown;
                }
                 static int Add( int number1, int number2)
                {
                         return number1 + number2;
                }
                 // Others are omitted here for brevity
        }

通过这个简单的例子,我们可以看出Switch通常会带来一些结构性的重复,通过利用Dictionary等二维表结构,我们可以尽量去避免switch语句,从而实现更好的灵活性和可维护性。最后需要支持的是,在Switch语句的重构过程中需要注意几点:
* 共通化以使得各个case中调用函数的签名一致
* if...else if...else结构同switch在某些情况下具有互换性,也可以考虑这种方法
* switch语句的重构是也可以考虑State Pattern以及Strategy Pattern等设计模式。选择的时候可以考虑视具体情况而定。