Extract Method(提炼方法)
Extract Method方法
将函数中过长的代码提炼出来,然后组织在一起并独立到一个新的独立方法中,并让方法名称解释该方法
的用途.该方法最好之处是有效的减少了方法中的代码.
实例代码如下:
1 private string myName;
2 public void printPeople(int Age)
3 {
4 printFamily();
5 //无数代码//
6
7 //打印个人信息
8 Console.WriteLine("Name:" + myName);
9 Console.WriteLine("Age:" + Age);
10 }
2 public void printPeople(int Age)
3 {
4 printFamily();
5 //无数代码//
6
7 //打印个人信息
8 Console.WriteLine("Name:" + myName);
9 Console.WriteLine("Age:" + Age);
10 }
重构后的代码如下:
1 private string myName;
2 public void printPeople(int Age)
3 {
4 printFamily();
5 //无数代码//
6 printMyInfo(Age);
7 }
8
9 void printMyInfo(int Age)
10 {
11 Console.WriteLine("Name:" + myName);
12 Console.WriteLine("Age:" + Age);
13 }
2 public void printPeople(int Age)
3 {
4 printFamily();
5 //无数代码//
6 printMyInfo(Age);
7 }
8
9 void printMyInfo(int Age)
10 {
11 Console.WriteLine("Name:" + myName);
12 Console.WriteLine("Age:" + Age);
13 }
为什么要这样重构?当一个函数很大的时候,第一对代码的修改起来非常的不方便.
第二,会对你读代码有障碍,试想一下当你看到一个很多行代码的方法,你还有心情看下去吗?
第三,方法与方法之间的复用性会非常的好,方法的重写也会更容易些.
那么我们应该怎么做呢?
看第一个例子:
无局部变量的方法提炼.
1 void printOwing()
2 {
3 ArrayList al = myOrders.GetOrderList();
4 double outstanding = 0.0;
5
6 //打印头部信息
7 Console.WriteLine("*****************");
8 Console.WriteLine("**Customer Owes**");
9 Console.WriteLine("*****************");
10
11 //计算
12 foreach(Object o in al)
13 {
14 Order each = (Order)o;
15 outstanding += each.Amount;
16 }
17
18 //打印具体信息
19 Console.WriteLine("Name:" + myName);
20 Console.WriteLine("Age:" + age);
21 }
2 {
3 ArrayList al = myOrders.GetOrderList();
4 double outstanding = 0.0;
5
6 //打印头部信息
7 Console.WriteLine("*****************");
8 Console.WriteLine("**Customer Owes**");
9 Console.WriteLine("*****************");
10
11 //计算
12 foreach(Object o in al)
13 {
14 Order each = (Order)o;
15 outstanding += each.Amount;
16 }
17
18 //打印具体信息
19 Console.WriteLine("Name:" + myName);
20 Console.WriteLine("Age:" + age);
21 }
好了我们开始先提最简单的部分.提出后的代码如下:
1 void printOwing()
2 {
3 ArrayList al = myOrders.GetOrderList();
4 double outstanding = 0.0;
5
6 printBanner();
7
8 //计算
9 foreach(Object o in al)
10 {
11 Order each = (Order)o;
12 outstanding += each.Amount;
13 }
14
15 //打印具体信息
16 Console.WriteLine("Name:" + myName);
17 Console.WriteLine("Age:" + age);
18 }
19
20 void printBanner()
21 {
22 //打印头部信息
23 Console.WriteLine("*****************");
24 Console.WriteLine("**Customer Owes**");
25 Console.WriteLine("*****************");
26 }
2 {
3 ArrayList al = myOrders.GetOrderList();
4 double outstanding = 0.0;
5
6 printBanner();
7
8 //计算
9 foreach(Object o in al)
10 {
11 Order each = (Order)o;
12 outstanding += each.Amount;
13 }
14
15 //打印具体信息
16 Console.WriteLine("Name:" + myName);
17 Console.WriteLine("Age:" + age);
18 }
19
20 void printBanner()
21 {
22 //打印头部信息
23 Console.WriteLine("*****************");
24 Console.WriteLine("**Customer Owes**");
25 Console.WriteLine("*****************");
26 }
最简单的提炼方法结束了.
下来我们看有局部变量的方法提炼.就拿上面的的代码开刀.
1 void printOwing()
2 {
3 ArrayList al = myOrders.GetOrderList();
4 double outstanding = 0.0;
5
6 printBanner();
7
8 //计算
9 foreach(Object o in al)
10 {
11 Order each = (Order)o;
12 outstanding += each.Amount;
13 }
14
15 printInfo(outstanding);
16 }
17
18 void printBanner()
19 {
20 //打印头部信息
21 Console.WriteLine("*****************");
22 Console.WriteLine("**Customer Owes**");
23 Console.WriteLine("*****************");
24 }
25
26 void printInfo(double OutStanding)
27 {
28 //打印具体信息
29 Console.WriteLine("Name:" + myName);
30 Console.WriteLine("Age:" + age);
31 }
2 {
3 ArrayList al = myOrders.GetOrderList();
4 double outstanding = 0.0;
5
6 printBanner();
7
8 //计算
9 foreach(Object o in al)
10 {
11 Order each = (Order)o;
12 outstanding += each.Amount;
13 }
14
15 printInfo(outstanding);
16 }
17
18 void printBanner()
19 {
20 //打印头部信息
21 Console.WriteLine("*****************");
22 Console.WriteLine("**Customer Owes**");
23 Console.WriteLine("*****************");
24 }
25
26 void printInfo(double OutStanding)
27 {
28 //打印具体信息
29 Console.WriteLine("Name:" + myName);
30 Console.WriteLine("Age:" + age);
31 }
我们再来看下对局部变量再赋值方法的提炼.继续拿上面代码开刀.
1 void printOwing()
2 {
3 double outstanding = GetOutStanding();
4
5 printBanner();
6
7 printInfo(outstanding);
8 }
9
10 void printBanner()
11 {
12 //打印头部信息
13 Console.WriteLine("*****************");
14 Console.WriteLine("**Customer Owes**");
15 Console.WriteLine("*****************");
16 }
17
18 void printInfo(double OutStanding)
19 {
20 //打印具体信息
21 Console.WriteLine("Name:" + myName);
22 Console.WriteLine("Age:" + age);
23 }
24
25 double GetOutStanding()
26 {
27 ArrayList al = myOrders.GetOrderList();
28 double outstanding = 0.0;
29 //计算
30 foreach(Object o in al)
31 {
32 Order each = (Order)o;
33 outstanding += each.Amount;
34 }
35 return outstanding
36 }
2 {
3 double outstanding = GetOutStanding();
4
5 printBanner();
6
7 printInfo(outstanding);
8 }
9
10 void printBanner()
11 {
12 //打印头部信息
13 Console.WriteLine("*****************");
14 Console.WriteLine("**Customer Owes**");
15 Console.WriteLine("*****************");
16 }
17
18 void printInfo(double OutStanding)
19 {
20 //打印具体信息
21 Console.WriteLine("Name:" + myName);
22 Console.WriteLine("Age:" + age);
23 }
24
25 double GetOutStanding()
26 {
27 ArrayList al = myOrders.GetOrderList();
28 double outstanding = 0.0;
29 //计算
30 foreach(Object o in al)
31 {
32 Order each = (Order)o;
33 outstanding += each.Amount;
34 }
35 return outstanding
36 }
Extract Method方法讲解玩了.有人会问为什么要这样写?这样写的好处我没有看到啊.
那么现在有个这样的需求,我要设置outstanding的初始值,那么我们只要修改GetOutStanding方法,代码
如下:
1 double GetOutStanding(double previousAmount)
2 {
3 ArrayList al = myOrders.GetOrderList();
4 double outstanding = previousAmount;
5 //计算
6 foreach(Object o in al)
7 {
8 Order each = (Order)o;
9 outstanding += each.Amount;
10 }
11 return outstanding
12 }
2 {
3 ArrayList al = myOrders.GetOrderList();
4 double outstanding = previousAmount;
5 //计算
6 foreach(Object o in al)
7 {
8 Order each = (Order)o;
9 outstanding += each.Amount;
10 }
11 return outstanding
12 }
主要方法修改如下:
1 void printOwing()
2 {
3 double outstanding = GetOutStanding(500.5);
4
5 printBanner();
6
7 printInfo(outstanding);
8 }
2 {
3 double outstanding = GetOutStanding(500.5);
4
5 printBanner();
6
7 printInfo(outstanding);
8 }