有关类中的重构问题

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


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


网站导航:
 
<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

留言簿(1)

文章分类(141)

文章档案(147)

搜索

最新评论