当对局部变量的引用无法使用Extract Method的时候,可以考虑将这个函数放入到一个单独的对象中去,如此一来,局部变量就变成了对象的字段,然后就可以在同一对象中将这个大型函数分解成小型函数。

如:在Order类中:

 

[java] view plain copy
 
  1. package com.xuzengqiang.ssb.movie;  
  2.   
  3. public class Order {  
  4.   
  5.     // .....  
  6.   
  7.     private double price() {  
  8.         double primaryBasePrice;  
  9.         double secondaryBasePrice;  
  10.         double tertiaryBasePrice;  
  11.         // do something  
  12.         return 1.0;  
  13.     }  
  14. }  

这个时候就可以将price抽取出来作为一个单独的类,此时里面的局部变量就变成了这个类里面的字段。

 

[java] view plain copy
 
  1. package com.xuzengqiang.ssb.movie;  
  2.   
  3. public class PriceCalculator {  
  4.   
  5.     private final Test test;  
  6.     private double primaryBasePrice;  
  7.     private double secondaryBasePrice;  
  8.     private double tertiaryBasePrice;  
  9.   
  10.     public PriceCalculator(Test test) {  
  11.         this.test = test;  
  12.     }  
  13.   
  14.     public double price() {  
  15.         double result = 0.0;  
  16.         // do something  
  17.         return result;  
  18.     }  
  19.   
  20.     // other ...  
  21. }  

 

 

做法:

 

1、建立一个新的class,根据待被处理的函数的用途给其命名。

2、在新的class中建立一个final值域,用来保存原先大型函数的对象,称为源对象。同时针对原函数的每个临时变量和每个参数,在新class中建立一个个对应的值域保存。

3、在新的class中建立一个构造函数,接收原对象及原函数的所有的参数作为参数。

4、在新的class中建立一个对应的方法,如上例中的price(),并将原函数中的代码拷贝进来。

5、编译,在原函数中利用new 新class().price()执行该方法。

例子:

 

[java] view plain copy
 
  1. public int gamma(int inputValue, int quantity, int yearToDate) {  
  2.     int importantValue1 = inputValue * quantity + delta();  
  3.     int importantValue2 = inputValue * yearToDate + 100;  
  4.     if ((yearToDate - importantValue1) > 100) {  
  5.         importantValue2 -= 20;  
  6.     }  
  7.     int importantValue3 = importantValue2 * 7;  
  8.     return importantValue3 - 2 * importantValue1;  
  9. }  

修改:

 

新建一个class,在这个class中提供一个final值域保存原先的对象,将源函数中的每一个参数和每一个临时变量,也以一个个值域逐一保存。

Gamma:

 

[java] view plain copy
 
  1. private final Test test;  
  2. private int inputValue;  
  3. private int quantity;  
  4. private int yearToDate;  
  5. private int importantValue1;  
  6. private int importantValue2;  
  7. private int importantValue3;  

提供对应的构造函数

 

 

[java] view plain copy
 
  1. public Gamma(Test test, int inputValue, int quantity, int yearToDate) {  
  2.     this.test = test;  
  3.     this.inputValue = inputValue;  
  4.     this.quantity = quantity;  
  5.     this.yearToDate = yearToDate;  
  6. }  

拷贝原gamma中的方法移动到新class中。整理之后就变成了:

 

 

[java] view plain copy
 
  1. package com.xuzengqiang.ssb.movie;  
  2.   
  3. public class Gamma {  
  4.   
  5.     private final Test test;  
  6.     private int inputValue;  
  7.     private int quantity;  
  8.     private int yearToDate;  
  9.     private int importantValue1;  
  10.     private int importantValue2;  
  11.     private int importantValue3;  
  12.   
  13.     public Gamma(Test test, int inputValue, int quantity, int yearToDate) {  
  14.         super();  
  15.         this.test = test;  
  16.         this.inputValue = inputValue;  
  17.         this.quantity = quantity;  
  18.         this.yearToDate = yearToDate;  
  19.     }  
  20.   
  21.     public int execute() {  
  22.         importantValue1 = inputValue * quantity + test.delta();  
  23.         importantValue2 = inputValue * yearToDate + 100;  
  24.         if ((yearToDate - importantValue1) > 100) {  
  25.             importantValue2 -= 20;  
  26.         }  
  27.         importantValue3 = importantValue2 * 7;  
  28.         return importantValue3 - 2 * importantValue1;  
  29.     }  
  30. }  

原函数中的gamma方法就变成了:

 

 

[java] view plain copy
 
  1. public int gamma(int inputValue, int quantity, int yearToDate) {  
  2.     return new Gamma(this,inputValue,quantity,yearToDate).execute();  
  3. }  

当然你还可以继续对execute()中的方法进行重构。

posted on 2018-02-06 10:15  Sharpest  阅读(263)  评论(0)    收藏  举报