有关类中的重构问题

类中可以将按照Constraint, Process and Specification进行重构

例如:
public class Bookshelf {
private int capacity = 20;
private Collection content;
public void add(Book book) {
if(content.size() + 1 <= capacity)
{
content.add(book);
} else {
throw new
IllegalOperationException(
“The bookshelf has reached
its limit.”);
}
}
}
We can refactor the code, extracting the constraint in a separate
method.
public class Bookshelf {
private int capacity = 20;
private Collection content;
public void add(Book book) {
if(isSpaceAvailable()) {
content.add(book);
} else {
throw new
IllegalOperationException(
“The bookshelf has reached
its limit.”);
}
}
private boolean isSpaceAvailable() {
return content.size() < capacity;
}
}

posted on 2007-10-18 12:07 刘铮 阅读(199) 评论(0)  编辑  收藏 所属分类: JAVA General

<2025年5月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

导航

统计

留言簿(1)

文章分类(141)

文章档案(147)

搜索

最新评论