您可以轻松实现抽象生成器类,我要做的是首先创建一个Abstract Person
和Parent
类将继承的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();
}
}
一旦有了这个,我就创建我的Parent
和Child
类,不确定在您的代码中继承Child
从Parent
继承是正确的。 / 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