我有一个看起来像这样的不可变类:
final class Foo {
private final String name;
private final MutableObject mo;
public Foo(String name, MutableObject mo) {
mo = mo.clone();
if(!Foo.testValidity(mo)) // this test is very expensive
throw new IllegalArgumentException();
this.name = name;
this.mo = mo;
}
public Foo bar(Foo that) {
return new Foo(this.name, that.mo);
}
}
该方法通过混合两个现有对象的内部来bar
返回一个对象。因为已经在一个对象中,所以保证它是有效的并且不需要复制或验证(构造函数当前所做的)。Foo
Foo
MutableObject
Foo
因为验证(可能还有克隆?)是昂贵的,如果可能的话,我想避免它们。最好的方法是什么?这就是我想出的:
final class Foo {
private final String name;
private final MutableObject mo;
public Foo(String name, MutableObject mo) {
this(name, mo, VerificationStyle.STRICT);
}
private Foo(String name, MutableObject mo, VerificationStyle vs) {
if(vs == VerificationStyle.STRICT) {
mo = mo.clone();
if(!Foo.testValidity(mo)) // this test is very expensive
throw new IllegalArgumentException();
}
this.name = name;
this.mo = mo;
}
public Foo bar(Foo that) {
return new Foo(this.name, that.mo, VerificationStyle.LENIENT);
}
private static enum VerificationStyle { STRICT, LENIENT; }
}
我认为,至少,它比使用虚拟参数更清晰/更清晰,并且比交换订单更不容易出错,但是有没有更好的方法来做到这一点?这通常如何实现?