Use Builder pattern from constructor in subclass

I am currently using the Builder template, closely monitoring the Java implementation proposed in the Builder template of the Wikipedia article http://en.wikipedia.org/wiki/Builder_pattern

This is a sample code that illustrates 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 found very similar building code, so I thought of subclassing MyPrimitiveObject in MySophisticatedObject and moving all my repeating code to its constructor .. and here is the problem,

How can I call the constructor of a superclass and assign its return 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; } } 
+5
java builder
Apr 10 '12 at 16:15
source share
2 answers

You might want to consider having a nested MySophisticatedObject.Builder that extends MyPrimitiveObject.Builder and overrides its build() method. Create a protected constructor in the constructor to accept the instance for which you want 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. } } 
+6
Apr 10 2018-12-12T00:
source share

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() } } 
+1
Apr 10 2018-12-12T00:
source share



All Articles