DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Frameworks
  4. 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 · Interview
Like (0)
Save
Tweet
Share
10.60K 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

  • Configure Kubernetes Health Checks
  • Integrate AWS Secrets Manager in Spring Boot Application
  • A Beginner’s Guide To Styling CSS Forms
  • Java REST API Frameworks

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: