我有一个特定的类结构,看起来像这样:
class Parent {
public Parent(int property) { /* use property */}
}
class Son extends Parent {
public Son(int parentProperty, String sonProperty) {
super(parentProperty);
/* use son property */
}
}
我想为这两个类创建构建器,以便:
class ParentBuilder {
protected int parentProperty;
public ParentBuilder parentProperty(int parentPropertyValue) {
parentPropertyValue = parentPropertyValue;
return this;
}
public Parent build() {
return new Parent(parentProperty);
}
}
class SonBuilder extends ParentBuilder {
private String sonProperty;
public SonBuilder sonProperty(String sonProperty) {
this.sonProperty = sonProperty;
return this;
}
@Override
public Son build() {
return new Son(parentProperty, sonProperty);
}
}
但这会导致以下问题:
SonBuilder sonBuilder = new SonBuilder();
sonBuilder.sonProperty("aString").build(); // this works and creates Son
sonBuilder.sonProperty("aString").parentProperty(1).build(); // this works and creates Parent instead of Son
sonBuilder.parentProperty(1).sonProperty("aString").build(); // this doesn't work
我意识到我在吹毛求疵,这可以通过不返回this
(即没有方法链接(来解决,但我想知道是否有一个优雅的解决方案。
编辑
似乎"优雅"这个词有点混乱。
我所说的"优雅"是指允许方法链接且不涉及铸造的解决方案。
第一点
sonBuilder.sonProperty("aString").parentProperty(1).build();
这有效并创建父而不是儿子
预计parentProperty()
返回一个ParentBuilder
:
public ParentBuilder parentProperty(int parentPropertyValue) {...
ParentBuilder.build()
创建了一个Parent
:
public Parent build() {
return new Parent(parentProperty);
}
第二点
sonBuilder.parentProperty(1).sonProperty("aString").build(); // this doesn't work
如第一点所述,parentProperty()
返回一个ParentBuilder
。
当然,ParentBuilder
没有sonProperty()
的方法。
所以它无法编译。
我想知道是否有一个优雅的解决方案。
一个优雅的解决方案不是SonBuilder
继承ParentBuilder
而是用ParentBuilder
字段进行组合。例如:
class SonBuilder {
private String sonProperty;
private ParentBuilder parentBuilder = new ParentBuilder();
public SonBuilder sonProperty(String sonProperty) {
this.sonProperty = sonProperty;
return this;
}
public SonBuilder parentProperty(int parentPropertyValue) {
parentBuilder.parentProperty(parentPropertyValue);
return this;
}
public Son build() {
return new Son(parentBuilder.parentProperty, sonProperty);
}
}
您可以通过这种方式创建Son
:
SonBuilder sonBuilder = new SonBuilder();
Son son = sonBuilder.sonProperty("aString").parentProperty(1).build();
我不确定它是否可以被认为是优雅的,但您可以使用强制转换:
SonBuilder sonBuilder = new SonBuilder();
Son son1 = sonBuilder.sonProperty("aString").build();
Son son2 = (Son) sonBuilder.sonProperty("aString").parentProperty(1).build();
Son son3 = ((SonBuilder) sonBuilder.parentProperty(1)).sonProperty("aString").build();
可以测试父属性是否存在于build()
方法中。
return (parentProperty == null) ? new Parent(parentProperty, sonProperty) : new Son(sonProperty);
覆盖SonBuilder
中的parentProperty
:
@Override
public SonBuilder parentProperty(int parentPropertyValue) {
super.parentProperty(parentPropertyValue);
return this;
}
这是安全的,因为(只要(super.parentProperty
返回this
。如果您有不可变的构建器,则您的覆盖也会有所不同。
或使用泛型:
abstract class AParentBuilder<T extends Parent> {
protected int parentProperty;
public AParentBuilder<T> parentProperty(int parentPropertyValue) {
parentPropertyValue = parentPropertyValue;
return this;
}
abstract public T build();
}
class ParentBuilder extends AParentBuilder<Parent> {
@Override
public Parent build() {
return new Parent(parentProperty);
}
}
abstract class ASonBuilder<T extends Son> extends AParentBuilder<T> {
private String sonProperty;
public ASonBuilder<T> sonProperty(String sonProperty) {
this.sonProperty = sonProperty;
return this;
}
}
class SonBuilder extends ASonBuilder<Son> {
@Override
public Son build() {
return new Son(parentProperty, sonProperty);
}
}
另一种方法是在子类方法中使用协变返回类型。
保留SonBuilder
扩展ParentBuilder
但覆盖parentProperty()
以返回SonBuilder
:
@Override
public SonBuilder parentProperty(int parentPropertyValue) {
super.parentProperty(parentPropertyValue);
return this;
}
您仍然可以通过以下方式创建儿子:
SonBuilder sonBuilder = new SonBuilder();
Son son = sonBuilder.sonProperty("aString").parentProperty(1).build();
就像现在一样,parentProperty(1)
调用返回SonBuilder
的子类方法。
这个解决方案可能看起来不错,但它有一个缺点。
它在子类生成器和基础生成器之间创建了强耦合。
每次在父生成器中添加 setProperty 方法时,都必须在子类中重写它。
否则,您仍然会遇到最初的问题:无法编译的代码,或者它将实例化父类而不是儿子类。
对于完全没有变化的类,这可能是可以接受的。
否则,应避免使用。
使用组合的解决方案更好,因为它允许SonBuilder
按照自己的节奏和规则发展。