DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Effective Java 2nd Edition – Builder Pattern in Eclipse

Effective Java 2nd Edition – Builder Pattern in Eclipse

Manuel Selva user avatar by
Manuel Selva
·
Apr. 10, 10 · Java Zone · Interview
Like (0)
Save
Tweet
10.31K Views

Join the DZone community and get the full member experience.

Join For Free

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/

Builder pattern Java (programming language) Eclipse

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A First Look at CSS When and Else Statements
  • ETL, ELT, and Reverse ETL
  • Fintech and AI: Ways Artificial Intelligence Is Used in Finance
  • How to Hash, Salt, and Verify Passwords in NodeJS, Python, Golang, and Java

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo