类中可以将按照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;
}
}