ivaneeo's blog

自由的力量,自由的生活。

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  669 Posts :: 0 Stories :: 64 Comments :: 0 Trackbacks
下一个步骤是找出程序中对于旧函数的所有引用(reference)点,并修改它们,让它们改用新函数. 下面是原本的程序:
class Customer...
public String statement() {
    double totalAmount = 0;
    int frequentRenterPoints = 0;
    Enumeration rentals = _rentals.elements();
    String result = "Rental Record for * " + getName() + "\n";
    while(rentals.hasMoreElements()) {
       double thisAmount = 0;
       Rental each = (Rental)rentals.nextElement();    //取得一笔租借记录

       thisAmount = amountFor(each);

       //   add frequent renter points(累加常客积点)
          frequentRenterPoints ++;
          if((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) &&
             each.getDaysRented() > 1)
             frequentRenterPoints ++;

          result += "\t" + each.getMovie().getTitle() + "\t" +
             String.valueOf(thisAmount) + "\n";
          totalAmount += thisAmount;
    }
//   add footer lines(结尾打印)
    result += "Amount owed is " + String.valueOf(totalAmount) + " \n";
    result += "You earned " + String.valueOf(frequentRenterPoints) +
       "frequent renter points";
    return result;
}

本例之中,这个步骤很简单,因为我才刚刚产生新函数,只有一个地方使用了它.一般情况下你得在可能运用该函数的所有classes中查找一遍.
class Customer
public String statement() {
    double totalAmount = 0;
    int frequentRenterPoints = 0;
    Enumeration rentals = _rentals.elements();
    String result = "Rental Record for * " + getName() + "\n";
    while(rentals.hasMoreElements()) {
       double thisAmount = 0;
       Rental each = (Rental)rentals.nextElement();    //取得一笔租借记录

       thisAmount = each.getCharge();

       //   add frequent renter points(累加常客积点)
          frequentRenterPoints ++;
          if((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) &&
             each.getDaysRented() > 1)
             frequentRenterPoints ++;

          result += "\t" + each.getMovie().getTitle() + "\t" +
             String.valueOf(thisAmount) + "\n";
          totalAmount += thisAmount;
    }
//   add footer lines(结尾打印)
    result += "Amount owed is " + String.valueOf(totalAmount) + " \n";
    result += "You earned " + String.valueOf(frequentRenterPoints) +
       "frequent renter points";
    return result;
}
posted on 2005-08-15 13:00 ivaneeo 阅读(201) 评论(0)  编辑  收藏 所属分类: refactoring-从地狱中重生

只有注册用户登录后才能发表评论。


网站导航: