使用Action表驱动代替switch…case语句
大家都知道,代码中出现过多的if…else嵌套语句时很难读,所以常用switch…case语句进行代替,代码的可读性就好了很多。
举个简单的例子:
public void DoSomeThing( string str) { if (str == "A" ) { DoSomethingForA(str); } else if (str == "B" ) { DoSomethingForB(str); } else if (str == "C" ) { DoSomethingForC(str); } } |
我们一眼就看出,这段代码可以使用switch语句进行重构,重构后的代码如下:
public void DoSomeThing( string str) { switch (str) { case "A" : DoSomethingForA(str); break ; case "B" : DoSomethingForB(str); break ; case "C" : DoSomethingForC(str); break ; default : break ; } } |
这样看下去,代码确实清晰了很多.这种重构方法我们很多人都会这么做,而且效果还不错.上面的例子中代码行还很少,实际当中有可能会出现很多的case语句, 即使我们这样重构,在阅读的时候还是需要拉很长的滚动条才可以看到需要找的case语句对应的代码.所以,针对这种情况,我们还可以用更简洁的方法进行重构. 实现起来也挺简单的,即:建立全局的字典类型的变量,保存不同的case语句条件以及对应的方法,这样,在需要调用的时候直接查找到该方法,invoke即可,其实也是用了委托来实现.看以下代码:
private static Dictionary< string , Action< string >> dict = new Dictionary< string , Action< string >>(); public BuildDictionary() { dict.Add( "A" , DoSomethingForA); dict.Add( "B" , DoSomethingForB); dict.Add( "C" , DoSomethingForC); } |
上面是声明变量及填充字典,做好这部分的工作之后,调用起来就方便多了,同样的DoSomeThing方法,代码就少了很多
public void DoSomeThing( string str) { if (dict.ContainsKey(str)) { Action< string > action = dict[str]; action.Invoke(str); } } |
重构之后,是不是简洁了很多,即使再多的case语句,我们也只需要在BuildDictionary方法中把它添加进去就可以了。
那么,是不是所有的switch…case语句都可以这样重构呢,以这样的实现方式来看,需要case条件对应的方法参数类型相同,这样才容易构造出字典来保存。
如果方法还要返回值,那我们就不能用Action,需要用Func表达式来保存,因为它可以带有返回值。
SunnyUI.Net:C# .Net WinForm开源控件库、工具类库、扩展类库、多页面开发框架;Sunny's blog:https://www.cnblogs.com/yhuse