Java 使用不可变flyweights跳过多余验证
我有一个不可变的类,看起来像这样:Java 使用不可变flyweights跳过多余验证,java,constructor,immutability,flyweight-pattern,Java,Constructor,Immutability,Flyweight Pattern,我有一个不可变的类,看起来像这样: 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 Illega
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; }
}
我认为,至少,它比使用伪参数更清晰,比交换顺序更不容易出错,但是有更好的方法吗?这通常是如何实现的?可能会完全隐藏构造函数,并使用类似工厂的方法创建新实例,例如:
private Foo(String name, MutableObject mo) {
this.name = name;
this.mo = mo;
}
public Foo bar(Foo that) {
return new Foo(this.name, that.mo);
}
public static Foo create(String name, MutableObject mo) {
mo = mo.clone();
if(!Foo.testValidity(mo)) // this test is very expensive
throw new IllegalArgumentException();
return new Foo(name, mo);
}
我仍然坚持你的第一种方法,它看起来更简单。为了满足您跳过mo的
测试的要求,我希望在mo
对象中添加一个默认值为false
的字段,比如isValid
,并在第一次调用testValidity
方法后将其设置为true
。随后调用testValidity
方法将首先检查此isValid
标志,并且仅当isValid
为false
时才会继续测试。