use Builder pattern from the constructor in a subclass

此生再无相见时 提交于 2019-11-29 21:15:36

问题


I am currently using the Builder pattern, following closely the Java implementation suggested in the Wikipedia article Builder pattern http://en.wikipedia.org/wiki/Builder_pattern

This is a sample code that ilustrates my implementation

public class MyPrimitiveObject {
  private String identifier="unknown";
  public static class Builder {
    private final MyPrimitiveObject obj = new MyPrimitiveObject();
    public MyPrimitiveObject build() { return obj; }
    public Builder setidentifier (String val) {
     obj.identifier = val;
     return this;
    }
  }
  public static Builder createBuilder() { return new Builder(); }
  @Override public String toString() { return "ID: "+identifier; }
}

In some of my applications that use this class, I happen to find very similar building code , so I thought to subclass MyPrimitiveObject in MySophisticatedObject and move all my repeated code into its constructor.. and here is the problem.

How may I invoke the superclass Builder and assign its returned object as my instance?

public class MySophisticatedObject extends MyPrimitiveObject {
  private String description;
  public MySophisticatedObject (String someDescription) {
    // this should be the returned object from build() !!
    Builder().setidentifier(generateUUID()).build()
    description = someDescription;
  }     
}

回答1:


You might want to consider having a nested MySophisticatedObject.Builder which extends MyPrimitiveObject.Builder, and overrides its build() method. Have a protected constructor in the builder to accept the instance on which to set values:

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 code to set the description etc.
  }
}



回答2:


You need:

public class MySophisticatedObject extends MyPrimitiveObject {
  private String description;

  public static class SofisitcatedBuilder extends Builder {
    private final MySophisticatedObject obj = new MySophisticatedObject();
    public MyPrimitiveObject build() { return obj; }
    public Builder setDescription(String val) {
     obj.description = val;
     return this;
    }
  }

  public MySophisticatedObject (String someDescription) {
    // this should be the returned object from build() !!
    return new SofisitcatedBuilderBuilder()
         .setDescription(someDescription)
         .setidentifier(generateUUID()).build()
  }     
}


来源:https://stackoverflow.com/questions/10092458/use-builder-pattern-from-the-constructor-in-a-subclass

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!