15-1.메서드를 메서드 객체로 전환 Replace Method with Method Object

긴 메소드가 있는데, 지역변수 때문에 Extract Method를 적용할 수 없는 경우에는,
메소드를 그 자신을 위한 객체로 바꿔서 모든 지역변수가 그 객체의 필드가 되도록 한다. 이렇게 하면 메소드를 같은 객체 안의 여러 메소드로 분해할 수 있다.

before
class Account ....
   int gamma (int inputVal, int quantity, int yearToDate) {
      int importantValue1 = (inputVal * quantity) + delta();
      int importantValue2 = (inputVal * yearToDate) + 100;
      if ((yearToDate - importantValue1) > 100)
         importantValue2 -= 20;
      int importantValue3 = importantValue2 * 7;
      // 기타 등등
      return importantValue3 - 2 * importantValue1;
   }

위의 Account 클래스의 gamma 메서드를 아래와 객체로 만든다.

after-1
class Account ....
   int gamma (int inputVal, int quantity, int yearToDate) {
     return new Gamma(this, inputVal, yearToDate).compute(); 
   }
after-2
 class Gamma ...
   private readonly Account _account;
   private int inputVal;
   private int quantity;
   private int yearToDate;
   private int importantValue1;
   private int importantValue2;
   private int importantValue3;
   
   Gamma(Account source, int inputValArg, int quantityArg, int yearToDateArg) {
      _account = source;
      inputVal = inputValArg;
      quantity = quantityArg;
      yearToDate = yearToDateArg;
   }
   
   int compute() {
      int importantValue1 = (inputVal * quantity) + _account.delta();
      int importantValue2 = (inputVal * yearToDate) + 100;
      if ((yearToDate - importantValue1) > 100)
         importantValue2 -= 20;
      int importantValue3 = importantValue2 * 7;
      // 기타 등등
      return importantValue3 - 2 * importantValue1;

   }

출처: http://ggotae.tistory.com/entry/Replace-Method-with-Method-Object