2

我有一个看起来像这样的不可变类:

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返回一个对象。因为已经在一个对象中,所以保证它是有效的并且不需要复制或验证(构造函数当前所做的)。FooFooMutableObjectFoo

因为验证(可能还有克隆?)是昂贵的,如果可能的话,我想避免它们。最好的方法是什么?这就是我想出的:

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; }
}

我认为,至少,它比使用虚拟参数更清晰/更清晰,并且比交换订单更不容易出错,但是有没有更好的方法来做到这一点?这通常如何实现?

4

1 回答 1

1

也许完全隐藏构造函数并使用类似工厂的方法创建新实例,例如:

  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);
  }
于 2016-02-09T10:54:57.937 回答