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) {
java builder
PA. Apr 10 '12 at 16:15 2012-04-10 16:15
source share