移动方法(Move Method)是指把类中的方法移动到更加合适、合理的地方,特别是当该方法放在其他类中会比放在原来的类中更加经常被调用的时候。下面的代码例子:

 
  
  1. class BankAccount {  
  2.     private int accountAge;  
  3.     private int creditScore;  
  4.     private double interest;  
  5.       
  6.     public int getAccountAge() {  
  7.         return accountAge;  
  8.     }  
  9.  
  10.     public void setAccountAge(int accountAge) {  
  11.         this.accountAge = accountAge;  
  12.     }  
  13.  
  14.     public int getCreditScore() {  
  15.         return creditScore;  
  16.     }  
  17.  
  18.     public void setCreditScore(int creditScore) {  
  19.         this.creditScore = creditScore;  
  20.     }  
  21.  
  22.     public double getInterest() {  
  23.         return interest;  
  24.     }  
  25.  
  26.     public void setInterest(double interest) {  
  27.         this.interest = interest;  
  28.     }  
  29.  
  30.     public BankAccount(int accountAge, int creditScore, double interest) {  
  31.         this.accountAge = accountAge;  
  32.         this.creditScore = creditScore;  
  33.         this.interest = interest;  
  34.     }  
  35.       
  36.     // 计算利率  
  37.     public double calculateInteresRate() {  
  38.         if(creditScore > 500) {  
  39.             return 0.02;  
  40.         }else if(accountAge > 10) {  
  41.             return 0.03;  
  42.         }  
  43.         return 0.05;  
  44.     }  

 

 
  
  1. class AccountInterest {  
  2.     private BankAccount account;  
  3.       
  4.     public AccountInterest(BankAccount account) {  
  5.         this.account = account;  
  6.     }  
  7.       
  8.     public double insterestRate(){  
  9.         return account.calculateInteresRate();  
  10.     }  
  11.       
  12.     public boolean introductoryRate(){  
  13.         return (account.calculateInteresRate() < 0.05);  
  14.     }  

由于 AccountInterest 类比起 BankAccount 类更加频繁地调用计算利率的方法 calculateInteresRate() 。也就是说,该方法放置在 AccountInterest 类中更符合业务逻辑。那么重构的方式就是将 calculateInteresRate() 方法移动到 AccountInterest 类中即可,如下: 

 
  
  1. class BankAccount {  
  2.     private int accountAge;  
  3.     private int creditScore;  
  4.     private double interest;  
  5.       
  6.     public int getAccountAge() {  
  7.         return accountAge;  
  8.     }  
  9.  
  10.     public void setAccountAge(int accountAge) {  
  11.         this.accountAge = accountAge;  
  12.     }  
  13.  
  14.     public int getCreditScore() {  
  15.         return creditScore;  
  16.     }  
  17.  
  18.     public void setCreditScore(int creditScore) {  
  19.         this.creditScore = creditScore;  
  20.     }  
  21.  
  22.     public double getInterest() {  
  23.         return interest;  
  24.     }  
  25.  
  26.     public void setInterest(double interest) {  
  27.         this.interest = interest;  
  28.     }  
  29.  
  30.     public BankAccount(int accountAge, int creditScore, double interest) {  
  31.         this.accountAge = accountAge;  
  32.         this.creditScore = creditScore;  
  33.         this.interest = interest;  
  34.     }  

 

 
  
  1. class AccountInterest {  
  2.     private BankAccount account;  
  3.       
  4.     public AccountInterest(BankAccount account) {  
  5.         this.account = account;  
  6.     }  
  7.       
  8.     public double insterestRate(){  
  9.         return account.calculateInteresRate();  
  10.     }  
  11.       
  12.     public boolean introductoryRate(){  
  13.         return (account.calculateInteresRate() < 0.05);  
  14.     }  
  15.       
  16.     // 计算利率  
  17.     public double calculateInteresRate() {  
  18.         if(account.getCreditScore() > 500) {  
  19.             return 0.02;  
  20.         }else if(account.getAccountAge() 10) {  
  21.             return 0.03;  
  22.         }  
  23.         return 0.05;  
  24.     }  

看起来很简单的一种重构方法,就是“移动”,把一段方法代码移动到更加合适的地方。但是这并不容易做到,因为我们要判断到底哪个方法处于哪个类中才更合适,这个评判、衡量的尺度怎样把握,并不容易做到。此外,我认为虽然方法移动了,但是最终调用的时候还是通过原来类的对象来作为条件语句中的判断,也是依赖了原来的类,上面例子中的account.getCreditScore() &gt; 500 和 account.getAccountAge() &gt; 10 就是这样。只不判断之后的真正业务处理(计算利率)才由重构后的类来实现。


原文请参考以下链接:http://www.lostechies.com/blogs/sean_chambers/archive/2009/08/02/refactoring-day-2-move-method.aspx