带继承的Java Builder模式

时间:2017-08-28 18:55:49

标签: java inheritance design-patterns builder

我想实现一个带有静态内部类的构建器模式,比如说类A带有字段(a1,a2,a3),B带有字段(b1,b2),C带有字段(c1),而所有共享字段(来自超类SuperClass的s1,s2:

public class A extends SuperClass {
    private final String a1;
    ...

    private A(ABuilder builder) {
        super(builder);
        this.a1 = builder.a1;
        ...
    }

    public static class ABuilder extends SuperClassBuilder implements ABuilderInterface {
        private String a1;
        ...

        @Override
        public ABuilder withA1(String a1) {
            this.a1 = a1;
            return this;
        }
        ...

        @Override
        public SuperClass build() {
            return new A(this);
        }
    }
}

因此,对于B和C,构建器只是不同,它们有自己的字段并实现它们自己的接口(BBuilderInterface和CBuilderInterface),而这些接口只定义要实现的方法:

public interface ABuilderInterface extends SuperClassBuilderInterface {
    ABuilderInterface withA1(String a1);
    ...
}
...<interfaces for B and C>

public interface SuperClassBuilderInterface {
   SuperClassBuilderInterface withS1(String s1);
   ...
   SuperClass build();
}

// Usage of the builders:
public SuperClass foo() {
    return new A.ABuilder()
        .withA1(...) // returns ABuilderInterface
        ...
        .withS1(...) // returns SuperClassBuilderInterface
        ...
        .build();
}

public abstract class SuperClass {
private final String s1;
...

protected SuperClass(SuperClassBuilder builder) {
    this.s1 = builder.s1;
    ...
}

protected static abstract class SuperClassBuilder implements SuperClassBuilderInterface {
    private String s1;
    ...

    @Override
    public SuperClassBuilder withS1(String s1) {
        this.s1 = s1;
        return this;
    }
    ...

    @Override
    public abstract SuperClass build();
}
}

现在你可以发现当我使用构建器时我必须注意首先调用与子类相关的with ...方法的限制,然后将它们链接到超类,这不是一个大的一笔交易,但仍然不确定是否良好的做法。 另一方面,我可以将子类的with ...方法一起添加到超类接口,然后限制消失,但后来我有一个混合了...的不同子类的方法。< / p>

您更喜欢/建议哪一个?

1 个答案:

答案 0 :(得分:5)

修改超类的构建器以使用F-bound(又名奇怪的重复模板模式)。

public interface SuperClassBuilderInterface<SELF extends SuperClassBuilderInterface<SELF>> {
    SELF withS1(String s1);
    // etc.
    Superclass build();
}

然后你有:

class SuperClassBuilder<SELF extends SuperClassBuilder<SELF>> implements SuperClassBuilderInterface<SELF>

interface ABuilderInterface<SELF extends ABuilderInterface<SELF>> extends SuperClassBuilderInterface<SELF>

class ABuilder extends SuperClassBuilder<ABuilder> implements ABuilderInterface<ABuilder>

请注意,SuperClassBuilder的实现必须包含return (SELF)this;形式的未经检查的强制转换。类型系统在理论上足够强大,不需要这个,但结果编码可能会非常难看(参见this)并且它可能不值得。

编辑:这是@shmosel的意思