Effective Java 2nd Edition – Builder Pattern in Eclipse
Join the DZone community and get the full member experience.
Join For FreeI sometimes use the Builder pattern as defined in Effective Java 2nd Edition item 2. Incidentally, I recommend this book to everybody writing Java code. Because I am a lazy man, I love JDT’s Java templates and will be really happy to have such a template for the builder pattern.
Let’s take the following example of Builder in order to illustrate this post (I just pasted 3 of the many parameters of the circle renderer class for readability):
public class XYPointCircleRenderer extends AbstractPointRenderer {
/**
* Builder class for {@link XYPointCircleRenderer}.
*/
public static class Builder {
private boolean bordered = false;
private final int radius;
private boolean tooltipable = true;
/**
* Construct a new Builder with the given radius.
*
* @param radius
* the radius of the circle. Must be > 0.
* @throws IllegalArgumentException
* if radius <= 0
*/
public Builder(int radius) {
if (radius <= 0) {
throw new IllegalArgumentException(
"radius must be greater than 0");
}
this.radius = radius;
}
/**
* Set the bordered value of this builder.
*
* @param val
* the bordered value
* @return this for convenience
*/
public Builder bordered(boolean val) {
bordered = val;
return this;
}
/**
* Return a new {@link XYPointCircleRenderer}.
*
* @return a new {@link XYPointCircleRenderer}
*/
public XYPointCircleRenderer build() {
return new XYPointCircleRenderer(this);
}
/**
* Sets the tooltipable value of this builder.
*
* @param val
* the tooltipable value
* @return this for convenience
*/
public Builder tooltipable(boolean val) {
tooltipable = val;
return this;
}
}
/**
* The circle's diameter.
*/
private final int diam;
/**
* The circle's radius.
*/
private final int radius;
/**
* Private constructor use by Builder.
*
* @param builder
* the builder to use to set this attributes.
*/
private XYPointCircleRenderer(Builder builder) {
super(builder.bordered, builder.tooltipable);
radius = builder.radius;
diam = 2 * radius;
}
@Override
public ChartElement paintPoint(int x, int y, GC gc) {
gc.fillOval(x-radius, y-radius, diameter, diam);
if (bordered) {
gc.drawOval(x-radius, y-radius, diam, diam);
}
if (tooltipable) {
rect = new Rectangle(x-radius,y-radius,diam,diam);
return new RectChartEleme(null, rect);
} else {
return null;
}
}
}
I would like to create a builder template “asking for questions to the user”. I.e the builder template should ask the end user what are the required fields of the enclosing class, and what are the default values of non required fields. Is it possible to do that ? If yes, where can I get information to start ?
Note: I often use eclipse “equals and hashcode” generator, and I guess it’s implemented by Java code. Thus the only solution to my problem is may be to implement my Builder generator the same way ….
From http://manuelselva.wordpress.com/2010/04/09/effective-java-2nd-edition-builder-pattern-in-eclipse/
Opinions expressed by DZone contributors are their own.
Comments