Effective Java 2nd Edition – Builder Pattern in Eclipse
I 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/
- Login or register to post comments
- 6516 reads
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)




Comments
Vladimír Oraný replied on Sat, 2010/04/10 - 4:11am
First create base class with final fields with default values.
public class ClassWithBuilder { // 1st group private final int theNumber = 10; private final String theString = "default string"; }than create public static class Builder within the class. Also create private constructor of ClassWithBuilder which takes the Builder as a parameter and build method whithin Builder class. Copy fields declaration twice into Builder class body and once in constructor and build method.public static class Builder { // 2nd group private final int theNumber = 10; private final String theString = "default string"; // 3rd group private final int theNumber = 10; private final String theString = "default string"; public ClassWithBuilder build(){ // 4th group private final int theNumber = 10; private final String theString = "default string"; return new ClassWithBuilder(this); } } private ClassWithBuilder(Builder b) { // 5th group private final int theNumber = 10; private final String theString = "default string"; }And now comes the regex magic.Select the first group of fields open the find/replace dialog and ensure that you've selected Scope - Selected lines and Options - Regular experssions. Keeping search string the same apply following replacement for the other groups.
2nd group - private $2 $3 = $4; 3rd group - public Builder $3($2 val){ $3 = val; return this; } 4th group - if ($3 == null) { $3 = $4; } 5th group - this.$3 = b.$3;The 4th group checks objects for null, so you must delete lines for primitives and objects where null is allowed value. And voilà the class with builder is done:public class ClassWithBuilder { private final int theNumber; private final String theString; public static class Builder { private int theNumber = 10; private String theString = "default string"; public Builder theNumber(int val){ theNumber = val; return this; } public Builder theString(String val){ theString = val; return this; } public ClassWithBuilder build(){ return new ClassWithBuilder(this); } } private ClassWithBuilder(Builder b) { this.theNumber = b.theNumber; this.theString = b.theString; } }Manuel Selva replied on Mon, 2010/04/12 - 4:08am
in response to: musketyr85
Thanks for this tip. I'll try it, nevertheless as you said: "I would also prefer some fully automated solution"
Manu
Marceli Szpak replied on Sat, 2010/11/20 - 2:02pm
There's a small library make-it-easy which helps create Data Builders mostly for usage in tests.