分类目录

链接

2017 年 9 月
 123
45678910
11121314151617
18192021222324
252627282930  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > .NET > 正文
if else过多应该使用策略模式
.NET 暂无评论 阅读(767)
  1.  
  2.     public class Test
  3.     {
  4.  
  5.         private int type;
  6.         Dictionary<int, Action> actionList = new Dictionary<int, Action>();
  7.  
  8.         public Test(int _type)
  9.         {
  10.             type = _type;
  11.             actionList.Add(1, () => { /*todo 1 */ });
  12.             actionList.Add(2, () => { /*todo 2 */});
  13.             actionList.Add(3, () => { /*todo 3 */});
  14.             actionList.Add(4, () => { /*todo 4 */});
  15.  
  16.         }
  17.         public void Do()
  18.         {
  19.             var func = actionList[type];
  20.  
  21.             //执行方法
  22.             func();
  23.         }
  24.     }
  25.  
  26.     public class Project
  27.     {
  28.         public void Main()
  29.         {
  30.             var type = 1;
  31.             Test t = new Test(type);
  32.             t.Do();
  33.         }
  34.  
  35.     }

============ 欢迎各位老板打赏~ ===========

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:if else过多应该使用策略模式 | Bruce's Blog

发表评论

留言无头像?