# ivaneeo's blog

BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
 669 Posts :: 0 Stories :: 64 Comments :: 0 Trackbacks

void printOwing() {
Enumeration e = _orders.elements();
double outstanding = 0.0;

printBanner();

// calculate outstanding
while(e.hasMoreElements()) {
Order each = (Order) e.nextElement();
outstanding += each.getAmount();
}

printDetails(outstanding);
}

void printOwing() {
printBanner();
double outstanding = getOutstanding();
printDetails(outstanding);
}

double getOutstanding() {
Enumeration e = _orders.elements();
double outstanding = 0.0;

while(e.hasMoreElements()) {
Order each = (Order) e.nextElement();
outstanding += each.getAmount();
}
return outstanding;
}

Enumeration变量e只在被提炼码中用到,所以我可以将它整个搬到新函数中.double变量outstanding在被提炼码内外都被用到,所以我必须让提炼出来的新函数返回它.编译测试完成后,我就把传值改名,遵循我的一贯命名原则:

double getOutstanding() {
Enumeration e = _orders.elements();
double result = 0.0;

while(e.hasMoreElements()) {
Order each = (Order) e.nextElement();
result += each.getAmount();
}
return result;
}

void printOwing(double previousAmount) {
Enumeration e = _orders.elements();
double outstanding = previousAmount * 1.2;

printBanner();

// calculate outstanding
while(e.hasMoreElements()) {
Order each = (Order) e.nextElement();
outstanding += each.getAmount();
}

printDetails(outstanding);
}

void printOwing(double previousAmount) {
double outstanding = previousAmount * 1.2;

printBanner();
double outstanding = getOutstanding(outstanding);
printDetails(outstanding);
}
double getOutstanding(double initialValue) {
double result = initialValue;
Enumeration e = _orders.elements();

while(e.hasMoreElements()) {
Order each = (Order) e.nextElement();
result += each.getAmount();
}
return result;
}

void printOwing(double previousAmount) {
printBanner();
double outstanding = getOutstanding(previousAmount * 1.2);
printDetails(outstanding);
}

posted on 2005-08-24 16:14 ivaneeo 阅读(147) 评论(0)  编辑  收藏 所属分类: refactoring－从地狱中重生

 只有注册用户登录后才能发表评论。 网站导航: 相关文章: