有关类中的重构问题

类中可以将按照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 刘铮 阅读(192) 评论(0)  编辑  收藏 所属分类: JAVA General


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


网站导航:
 
<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

导航

统计

留言簿(1)

文章分类(141)

文章档案(147)

搜索

最新评论