您可能需要考虑使用嵌套的
MySophisticatedObject.Builderextends
MyPrimitiveObject.Builder,并覆盖其
build()方法。在构建器中具有受保护的构造函数,以 接受 要在其上设置值的实例:
public class MyPrimitiveObject { private String identifier="unknown"; public static class Builder { private final MyPrimitiveObject obj; public MyPrimitiveObject build() { return obj; } public Builder setidentifier (String val) { obj.identifier = val; return this; } public Builder() { this(new MyPrimitiveObject()); } public Builder(MyPrimitiveObject obj) { this.obj = obj; } } ...}public class MySophisticatedObject extends MyPrimitiveObject { private String description; public static class Builder extends MyPrimitiveObject.Builder { private final MySophisticatedObject obj; public Builder() { this(new MySophisticatedObject()); super.setIdentifier(generateUUID()); } public Builder(MySophisticatedObject obj) { super(obj); this.obj = obj; } public MySophisticatedObject build() { return obj; } // Add pre to set the description etc. }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)