学习之路二十九:泛型和委托在重构中的运用

最近在对项目中的代码进行重构,在重构的过程中发现了一些很有趣的东西,就是泛型和委托!

泛型和委托给我的重构带来了很大的便利,我也对它们在重构的技术上有了一些新的认识!

如果小菜说的不对,还请园友们帮我指出,我会虚心接受的,谢谢~!

下面我只是说泛型和委托在重构时的一些观点,不包含泛型和委托的所有知识点!

一丶泛型

重构前代码:

 1     public class Test
 2     {
 3         public object TestOne(int number)
 4         {
 5             switch (number) //根据传过来的number来选择对象
 6             {
 7                 case 1:
 8                     return Test1();
 9                 case 2:
10                     return Test2();
11                 default:
12                     return null;
13             }
14         }
15 
16         public Person1 Test1() //很简单,返回一个对象实例
17         {
18             Person1 person1 = new Person1();
19             return person1;
20         }
21 
22         public Person2 Test2() //这个也是返回一个对象实例
23         {
24             Person2 person2 = new Person2();
25             return person2;
26         }
27     }
28 
29     public class Person1
30     { }
31 
32     public class Person2
33     { }

分析:

  ①首先看到有两个方法的操作行为都是返回对象实例!

  ②也就是操作行为是相似的,但是返回的类型是不一样的!

  ③找到了相同点之后,我们就要对经常变化的点进行封装了,也就是我们要封装类型的变化点!

  ③最后我们很容易想到用泛型实现这样的需求!

重构后代码:

 1     public class Test
 2     {
 3         public object TestOne(int number)
 4         {
 5             switch (number)
 6             {
 7                 case 1:
 8                     return TestTwo<Person1>();
 9                 case 2:
10                     return TestTwo<Person2>();
11                 default:
12                     return null;
13             }
14         }
15 
16         public T TestTwo<T>() where T : class,new() //通过泛型来封装类型变化点
17         {
18             T t = new T();
19             return t;
20         }
21     }
22 
23     public class Person1
24     { }
25 
26     public class Person2
27     { }

结论:泛型封装类型变化点

通过泛型来传递类型,规定现在我们应该使用哪一种类型,这样就可以做到类型封装,从而做到以不变应万变!

 

二丶委托

重构前代码:

 1     public class Test
 2     {
 3         public void Test1()
 4         {
 6             DataTable table = new DataTable(); //获取数据源
 7             //生成Entity
 8             foreach (DataRow item in table.Rows)
 9             {
10                 GenerateEntity1(item);
11             }
12         }
13 
14         public void Test2()
15         {
17             DataTable table = new DataTable(); //获取数据源
18             //生成Entity
19             foreach (DataRow item in table.Rows)
20             {
21                 GenerateEntity2(item);
22             }
23         }
24 
25         public void GenerateEntity1(DataRow dataRow)
26         {
27             //生成Entity1
28         }
29 
30         public void GenerateEntity2(DataRow dataRow)
31         {
32             //生成Entity2
33         }
34     }

分析:

  ①首先会看到Test1和Test2方法中在生成Entity时会有foreach循环的重复代码!

  ②要想提取出两边的通用代码,需要分析两边哪些代码是一样的,哪些是变化的,这样我们就能提取出不变的代码,而用一些技术来封装变化点,最终做到通用!

  ③我们很容易发现foreach循环的代码是一样的,生成entity是不一样的,但是在仔细看一下生成entity方法的参数和返回值是一样的!

  ④那么怎么来封装那个经常变化的点呢?

  ⑤我们想到委托,通过委托来传递操作行为来做到变化!

重构后代码:

 1     public class Test2
 2     {
 3         public void Test1()
 4         {
 5             DataTable table = new DataTable(); //数据源
 6             GenerateEntity(table, GenerateEntity1);
 7         }
 8 
 9         public void Test2()
10         {
11             DataTable table = new DataTable();  //数据源,跟上面不同的数据源
12             GenerateEntity(table, GenerateEntity2);
13         }
14 
15         public void GenerateEntity1(DataRow dataRow)
16         {
17             //生成Entity1
18         }
19 
20         public void GenerateEntity2(DataRow dataRow)
21         {
22             //生成Entity2
23         }
24 
25         private void GenerateEntity(DataTable table, Action<DataRow> action)  //通过委托来实现操作行为的传递
26         {
27             foreach (DataRow item in table.Rows)
28             {
29                 action(item);
30             }
31         }
32     }

结论:委托封装行为操作变化点

通过传递委托实例来指定我需要使用那种操作行为,如果发现返回值不一样,那么再加上泛型来封装类型的变化点,最后就构成了委托和泛型的综合利用!

 

三丶总结

  其实就是想说在实践中进行思考,进行总结,最终一定会有不一样的收获! 

 

已同步至:个人文章目录索引

posted @ 2013-04-19 00:47  TimYang  阅读(1902)  评论(9编辑  收藏  举报