使用构建器模式扩展类

hoo*_*hal 3 java oop inheritance builder

我正在尝试将 a 类扩展到 aX。所以,我也扩展了 aBuilder。但是,虽然我能够使用以下方法创建类 a 的对象:

aBuilder f = new aBuilder();
f.bi = i;
f.bs = s;
a atry = f.withI(i).withS(s).build();
Run Code Online (Sandbox Code Playgroud)

这同样不适用于 aX。当我尝试这样做时:

aXBuilder fb = new aXBuilder();
aX aXtry = fb.withI(i).withS(s).withB(b).build();
Run Code Online (Sandbox Code Playgroud)

我收到错误消息(对于 a.aBuilder 类型,未定义 withB(Boolean) 方法)。我应该为 aX 重写所有内容,而不是简单地添加新内容吗?我不想这样做,因为这会导致我的代码中有很多重复。类 a 和 aX 如下所示:

class a {

protected String s;
protected int i;

public void getdata() {
    System.out.println(this.s);
    System.out.println(this.i);
}

protected a(aBuilder fb) {
    this.s = fb.bs;
    this.i = fb.bi;
}

public static class aBuilder {
    public aBuilder() {
    }

    protected String bs;
    protected int bi;

    public aBuilder withS(String s) {
        this.bs = s;
        return this;
    }

    public aBuilder withI(Integer i) {
        this.bi = i;
        return this;
    }

    public a build() {
        return new a(this);
    }

}
Run Code Online (Sandbox Code Playgroud)

}

类 aX 扩展了 {

protected Boolean b;

public void getData()
{
    System.out.println(this.s);
    System.out.println(this.i);
    System.out.println(this.b);
}

protected aX(aXBuilder axb) {
    super(axb);
    this.b = axb.bb;
}

public static class aXBuilder extends aBuilder {
    protected Boolean bb;

    public aXBuilder() {
    }

    public aXBuilder withB(Boolean b) {
        this.bb = b;
        return this;
    };

    public aX build() {
        return new aX(this);
    }
}
Run Code Online (Sandbox Code Playgroud)

}

Q23*_*Q23 5

您可以使用泛型解决您的问题,尽管它确实需要创建一个抽象超类。潜伏在这个网站上的经历告诉我,从具体类继承被广泛认为是邪恶的。

public abstract class AbstractA {
    protected String s;
    protected int i;
    protected AbstractA() {
    }
    protected abstract static class ABuilder<T extends AbstractA, B extends ABuilder<T,B>> {
        protected T object;
        protected B thisObject;
        protected abstract T getObject(); //Each concrete implementing subclass overrides this so that T becomes an object of the concrete subclass
        protected abstract B thisObject(); //Each concrete implementing subclass builder overrides this for the same reason, but for B for the builder
        protected ABuilder() {
            object = getObject();
            thisObject = thisObject();
        }
        public B withS(String s) {
            object.s = s;
            return thisObject;
        }
        public B withI(int i) {
            object.i = i;
            return thisObject;
        }
        public T build() {
            return object;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

一旦您将抽象类放在一起,您只需根据需要扩展它多次,覆盖构建器中的抽象方法以返回您需要的对象类型。

public final class ConcreteA extends AbstractA {
    private String foo;
    protected ConcreteA() {
    }
    public static final class Builder extends AbstractA.ABuilder<ConcreteA,Builder> {
        @Override protected ConcreteA getObject() {
            return new ConcreteA();
        }
        @Override protected Builder thisObject() {
            return this;
        }
        public Builder() {
        }
        public Builder withFoo(String foo) {
            object.foo = foo;
            return this;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后... ConcreteA baz = new ConcreteA.Builder().withFoo("foo").withS("bar").withI(0).build();