重构研究:Replace Method with Method Object(用方法对象代替方法)

动机:

如果一个函数的局部变量数量很多,那么分解这个函数是很困难的。虽然可以使用Replace Temp with Query(用查询函数代替临时变量)可以减少临时变量,但是有时候你发现无法拆解时,就要用到此方法

做法:

1.建立一个新类,根据要处理的函数的功能命名

2.如果有用到源对象中的函数,就要建立一个final字段,类型为源对象

3.把源函数中的临时变量,变为类的变量,建立构造函数,把源函数的临时变量传递进来

4.建立一个compute()函数

5.把源函数的内容拷贝到compute()函数中,如果有用到源函数中的其他函数调用,请使用第2步创建的源对象来调用

6.将旧函数的调用改为:创建一个新类对象,然后再调用新类对象的compute()函数

现在因为临时变量已经变成实例变量,就可以在新类的compute()函数中任意拆分了

举个例子: 

 1 public class Account {
 2 
 3     private int gamma(int inputVal, int quantity, int yearToDate) {
 4         int importantValue1 = (inputVal * quantity) + dalta();
 5         int importantValue2 = (inputVal * yearToDate) + 100;
 6         if ((yearToDate - importantValue1) > 100) {
 7             importantValue2 -= 20;
 8         }
 9         int importantValue3 = importantValue2 * 7;
10 
11         return importantValue3 - 2 * importantValue1;
12     }
13 }

 修改完后的代码如下:

 1 public class Account {
 2 
 3     private int gamma(int inputVal, int quantity, int yearToDate) {
 4         return new Gamma(this, inputVal, quantity, yearToDate).compute();
 5     }
 6 }
 7 
 8 class Gamma {
 9     private final Account account;
10     private int inputVal;
11     private int quantity;
12     private int yearToDate;
13 
14     public Gamma(Account source, int inputValArg, int quantityArg,
15             int yearToDateArg) {
16         this.account = source;
17         this.inputVal = inputValArg;
18         this.quantity = quantityArg;
19         this.yearToDate = yearToDateArg;
20     }
21     
22     public int compute() {
23         int importantValue1 = (inputVal * quantity) + this.account.dalta();
24         int importantValue2 = (inputVal * yearToDate) + 100;
25         if ((yearToDate - importantValue1) > 100) {
26             importantValue2 -= 20;
27         }
28         int importantValue3 = importantValue2 * 7;
29 
30         return importantValue3 - 2 * importantValue1;
31     }
32 }

接下来我就可以任意对compute()函数进行拆分了,完整代码如下

 1 public class Account {
 2 
 3     private int gamma(int inputVal, int quantity, int yearToDate) {
 4         return new Gamma(this, inputVal, quantity, yearToDate).compute();
 5     }
 6 
 7     public int dalta() {
 8         return 1;
 9     }
10 }
11 
12 class Gamma {
13     private final Account account;
14     private int inputVal;
15     private int quantity;
16     private int yearToDate;
17 
18     public Gamma(Account source, int inputValArg, int quantityArg,
19             int yearToDateArg) {
20         this.account = source;
21         this.inputVal = inputValArg;
22         this.quantity = quantityArg;
23         this.yearToDate = yearToDateArg;
24     }
25 
26     public int compute() {
27         return importantValue3() - 2 * importantValue1();
28     }
29 
30     private int importantValue3() {
31         return importantThing(importantValue2()) * 7;
32     }
33 
34     private int importantThing(int importantValue2) {
35         if ((yearToDate - importantValue1()) > 100) {
36             importantValue2 -= 20;
37         }
38         return importantValue2;
39     }
40 
41     private int importantValue2() {
42         return (inputVal * yearToDate) + 100;
43     }
44 
45     private int importantValue1() {
46         return (inputVal * quantity) + this.account.dalta();
47     }
48 }

 

posted on 2012-07-28 13:33  低调点  阅读(690)  评论(0)    收藏  举报

导航