在 Effective Java(第 2 版)的第 2 项中,作者提到了以下关于在使用构建器时对参数施加不变量的内容:
It is critical that they be checked after copying the parameters from the builder to the object, and that they be checked on the object fields rather than the builder fields (Item 39). If any invariants are violated, the build method should throw an IllegalStateException (Item 60).
这是否意味着在构建方法创建目标对象后,应该将其传递给验证例程以进行任何所需的验证?
此外,有人可以解释一下这背后的原因吗?
最佳答案
对象验证是使用构建器创建对象的组成部分。虽然您可以有一个单独的例程来执行验证,但不需要这样的分离:验证代码可以是执行构建的函数的一部分。换句话说,你可以这样做
TargetObject build() {
TargetObject res = new TargetObject();
res.setProperty1();
res.setProperty2();
validate(res); // This call may throw an exception
return res;
}
void validate(TargetObject obj) {
if (...) {
throw new IllegalStateException();
}
}
或者这个:
TargetObject build() {
TargetObject res = new TargetObject();
res.setProperty1();
res.setProperty2();
if (...) {
throw new IllegalStateException();
}
return res;
}
重要的是验证发生在目标对象构建之后,而不是之前。换句话说,您需要验证对象的状态,而不是构建器的状态。
关于java - 构建器模式验证 - Effective Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38173274/