JAVA中具有继承性的构建器模式

假设我有一个从另一个类继承的类:

class Parent{
    private String p1;
    public void setP1(String p1){
        this.p1 = p1;
    }
}
class Child extends Parent{
    private int c1;
    public void setC1(int c1){
        this.c1 = c1;
    }
}

我使用构建器模式创建父类的实例:


class ParentBuilder{
    aParent(){ return new ParentBuilder()}


    with(String p1){
        this.p1 = p1;
    }

    public Parent build(){
        Parent parent = new Parent();
        parent.setP1(this.p1);
        return parent;
    }
}

如何使用Child类的构建器模式做类似的事情

aParent().with("test").build();
aChild().with(4).build();
aChild().with(4).with("test").build();
aChild().with("test").with(4).build();
mmyxlong 回答:JAVA中具有继承性的构建器模式

您可以轻松实现抽象生成器类,我要做的是首先创建一个Abstract PersonParent类将继承的Child类。

public abstract class Person {

    private String p1;

    private Person(AbstractBasicBuilder builder) {
        this.p1 = builder.p1;
    }

    public void setP1(String p1) {
        this.p1 = p1;
    }

    public String getP1() {
        return p1;
    }

    public abstract static class AbstractBasicBuilder<T extends AbstractBasicBuilder> {

       private String p1;        

        AbstractBasicBuilder withP1(String p1) {
            this.p1 = p1;
            return this;
        }

        abstract Person build();        

    }
}

一旦有了这个,我就创建我的ParentChild类,不确定在您的代码中继承ChildParent继承是正确的。 / p>

public class Parent extends Person {

    private Parent(Builder builder) {
        super(builder);
    }

    public abstract static class Builder extends AbstractBasicBuilder<Person.Builder> {

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

现在我们有了Parent类,我们可以实现我们的Child类,在其中添加构建类,以便我们也可以提供父母。

public class Child extends Person {

    private Parent father;

    private Parent mother;

    private Child(Builder builder) {
        super(builder);
        this.father = builder.father;
        this.mother = builder.mother;
    }

    public Parent getFather() {
        return father;
    }

    public Parent getMother() {
        return mother;
    }

    public abstract static class Builder extends AbstractBasicBuilder<Child.Builder> {

        private Parent father;

        private Parent mother;         

        Builder withFather(Parent father) {
            this.father = father;
            return this;
        }

        Builder withMother(Parent mother) {
            this.mother = mother;
            return this;
        }

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

}

所以我们在这里所做的是,我们总共创建了3个类,我们的Person抽象类为我们实现了p1,因此我们不需要在多个类中重复代码。然后,我们有了抽象的构建器类,该类实现用于构建Person对象的基本代码。

然后,我们实现了Parent类,该类扩展了Person类,然后实现了AbstractBasicBuilder,它构造了Parent对象。

然后,我们实现了Child类,该类也扩展了Person-不是Parent,将来Child可能会变成Parent,但并非总是如此一个Parent-在本课程中,我们有2 i

,

如果您具有构建器所需的类层次结构,则应使用其自己的层次结构正确实施构建器。

正确的构建器实现不允许设置不属于所构建对象的字段。例如。如果Parent没有c1字段,则不应通过parentBuilder().withC1(val).build();

对其进行设置

例如,如果您具有以下对象层次结构:

        GrandPa
           |
        Parent
           |
          / \
    Child1   Child2

按照下面的代码创建构建器的层次结构是合理的:

带有构建器的GrandPa类:

public class GrandPa {
    private Integer gp;

    public void setGp(Integer gp) {
        this.gp = gp;
    }

    public static class GrandPaBuilder extends 
                GrandPaBuilderWithResult<GrandPaBuilder,GrandPa> {

        public static GrandPaBuilder grandPaBuilder() {
            return new GrandPaBuilder();
        }

        @Override
        public GrandPa build() {
            GrandPa grandPa = new GrandPa();
            grandPa.setGp(gp);

            return grandPa;
        }
    }

    public static abstract class GrandPaBuilderWithResult<T extends 
                GrandPaBuilderWithResult<T,R>,R extends GrandPa> {

        Integer gp;
        public T withGP(Integer gp) {
            this.gp = gp;
            return (T) this;
        }

        public abstract R build();
    }
}

带有构建器的父类:

public class Parent extends GrandPa {
    private String p;

    public void setP(String p) {
        this.p = p;
    }

    public static class ParentBuilder extends 
                ParentBuilderWithResult<ParentBuilder,Parent> {

        public static ParentBuilder parentBuilder() {
            return new ParentBuilder();
        }

        @Override
        public Parent build() {
            Parent parent = new Parent();
            parent.setGp(gp);
            parent.setP(this.p);

            return parent;
        }
    }

    public static abstract class ParentBuilderWithResult<T extends 
                ParentBuilderWithResult<T,R extends Parent> extends 
                GrandPaBuilderWithResult<T,R> {

        protected String p;

        public T withP(String p) {
            this.p = p;
            return (T) this;
        }
    }
}

以及带有构建器的子类:

public class Child1 extends Parent {

    private Double c1;

    public void setC1(Double c1) {
        this.c1 = c1;
    }

    public static class Child1Builder extends 
                ParentBuilderWithResult<Child1Builder,Child1> {

        private Double c1;

        public static Child1Builder child1Builder() {
            return new Child1Builder();
        }

        public Child1Builder withC1(Double c1) {
            this.c1 = c1;
            return this;
        }

        @Override
        public Child1 build() {
            Child1 child1 = new Child1();
            child1.setGp(gp);
            child1.setP(p);
            child1.setC1(this.c1);

            return child1;
        }
    }
}

public class Child2 extends Parent {

    private Double c2;

    public void setC2(Double c2) {
        this.c2 = c2;
    }

    public static class Child2Builder extends 
                ParentBuilderWithResult<Child2Builder,Child2> {

        private Double c2;

        public static Child2Builder child2Builder() {
            return new Child2Builder();
        }

        public Child2Builder withC2(Double c2) {
            this.c2 = c2;
            return this;
        }

        @Override
        public Child2 build() {
            Child2 child2 = new Child2();
            child2.setGp(gp);
            child2.setP(p);
            child2.setC2(this.c2);

            return child2;
        }
    }
}

请注意,在构建器中使用自绑定泛型时总是只返回所需的对象引用。 此实现是安全的,并且不允许通过构建器在代码中添加错误。

您可以通过以下方式使用它们:

GrandPa grandPa = GrandPa.GrandPaBuilder.grandPaBuilder().withGP(1).build();
Parent parent = Parent.ParentBuilder.parentBuilder().withGP(2).withP("p").build();
Child1 child1 = Child1.Child1Builder.child1Builder().withGP(3).withP("p1").withC1(1D).build();
Child2 child2 = Child2.Child2Builder.child2Builder().withGP(3).withP("p2").withC2(2D).build();
本文链接:https://www.f2er.com/3029908.html

大家都在问