简体   繁体   English

在扩展类上使用Builder构造函数?

[英]Using Builder Constructor on Extended Class?

I'm implementing a Builder constructor as documented in Joshua Bloch's "Effective Java 2nd Edition. However, I'm running into a few complications when I try to extend the class and its builder. Essentially, the extended Builder in the extended child class has set field methods that return the parent Builder type, not the child builder type. 我正在实现Joshua Bloch的“ Effective Java 2nd Edition”中记录的Builder构造函数。但是,当我尝试扩展该类及其生成器时,遇到了一些麻烦。本质上,扩展子类中的扩展Builder具有设置返回父构建器类型而不是子构建器类型的字段方法。

Of course, I can cast back to the ChildBuilder in the property build chain (as shown in my main method) but it is not seamless which defeats the purpose of the Builder, and it also forces me to segregate the parent setters and child setters. 当然,我可以回退到属性构建链中的ChildBuilder(如我的主要方法所示),但是它不是无缝的,这违背了Builder的目的,并且还迫使我将父级设置者和子级设置者分开。

I tried to use generics but it ended up becoming more verbose than the cast. 我尝试使用泛型,但最终变得比强制转换更为冗长。

Is there a way I can consistently make the set methods on the builders return the builder type that was actually instantiated? 有没有一种方法可以使构建器上的set方法始终返回实际实例化的构建器类型?

public class ParentObj {

    public static void main(String[] args) { 

    ChildObj childObj = ((ChildObj.ChildBuilder) (new ChildObj.ChildBuilder())
                .prop1(11)
                .prop2(21)
                .prop3(14))
                .prop4(12)
                .prop5(33)
                .build();
    }



    private int prop1;
    private int prop2;
    private int prop3;

    protected ParentObj(Builder builder) {
        this.prop1 = builder.prop1;
        this.prop2 = builder.prop2;
        this.prop3 = builder.prop3;
    }

    public class Builder { 
        private int prop1;
        private int prop2;
        private int prop3;
        public Builder prop1(int prop1) { this.prop1 = prop1; return this; } 
        public Builder prop2(int prop2) { this.prop2 = prop2; return this; } 
        public Builder prop3(int prop3) { this.prop3 = prop3; return this; } 

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

    }

    private class ChildObj extends ParentObj { 

    private final int prop4;
    private final int prop5;

    private ChildObj(ChildBuilder childBuilder) {
        super(childBuilder);

    }

    public class ChildBuilder extends Builder {
        private int prop4;
        private int prop5; 
        public ChildBuilder prop4(int prop4) { this.prop4 = prop4; return this; } 
        public ChildBuilder prop5(int prop5) { this.prop5 = prop5; return this; } 

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

Probably the best way would be to Override the parent builder methods. 最好的方法可能是重写父生成器方法。

class ChildBuilder {
  public ChildBuilder prop1(int prop1){ 
    return (ChildBuilder) super.prop1(prop1);
  }
}

While this isn't exactly clean it will work for what you're trying to do. 尽管这不是很干净,但可以满足您的尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM