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 > A Look at the Builder Pattern

A Look at the Builder Pattern

See some examples of the Builder pattern in action in order to create complicated objects with optional inputs.

Arun Pandey user avatar by
Arun Pandey
·
Dec. 05, 16 · Java Zone · Tutorial
Like (11)
Save
Tweet
29.49K Views

Join the DZone community and get the full member experience.

Join For Free

Builder is a part of the Creational pattern family, which is useful for creating complex objects with a lot of options — where all of them aren't necessary. In short, it's great when we need customized objects with optional inputs.

Let's take a real world example to understand it. A Subway shop is well-known for its customized sandwiches — as per the user inputs. The bread is the base of a sandwich, and toppings get added based on the user. So, as per the user inputs, Subway is customizing their sandwiches. That is exactly what we do when we use the builder pattern to build an object.

Let's look at implementing the pattern in Java to build the reports; as reports are something which need high level of customization.

IBuilder.java

public interface IBuilder {
  public void buildPreface();
  public void buildReportTitle();
  public void buildHeader();
  public void buildContent();
  public void buildFooter();
  public Report getReport();
}

Mybuilder.java

public class MyBuilder implements IBuilder{
  private Report report;

  public MyBuilder(){
    report = new Report();
  }

  @Override
  public Report getReport(){   
    return report;
  }

  @Override
  public void buildContent() {
    report.setContent("my 1st report............");
  }

  @Override
  public void buildFooter() {
    report.setFooter("copyright softwareArchitecture&DesignWithArun.com : 2014");
  }

  @Override
  public void buildHeader() {
    report.setHeader(new String[]{"header_1", "header_2"});
  }

  @Override
  public void buildPreface() {
    report.setPreface("Report using Builder by : "+System.getProperty("user.info"));
  }

  @Override
  public void buildReportTitle() {
    report.setReportTitle("My Report");
  }
}

ReportDirector.java

public class ReportDirector {

  private IBuilder myBuilder;

  public ReportDirector(){
    myBuilder = new MyBuilder();
  }
  public void buildReport(){
    // report building logic/steps
    myBuilder.buildReportTitle();
    myBuilder.buildHeader();
    myBuilder.buildPreface();
    myBuilder.buildContent();
    myBuilder.buildFooter();
  }

  public Report getReport(){
    return this.myBuilder.getReport();
  }
}

Report.java

public class Report {

  private String reportTitle;
  private String header[];
  private String preface;
  private String content;
  private String footer;

  public String getReportTitle() {
    return reportTitle;
  }
  public void setReportTitle(String reportTitle) {
    this.reportTitle = reportTitle;
  }
  public String[] getHeader() {
    return header;
  }
  public void setHeader(String[] header) {
    this.header = header;
  }
  public String getPreface() {
    return preface;
  }
  public void setPreface(String preface) {
    this.preface = preface;
  }
  public String getContent() {
    return content;
  }
  public void setContent(String content) {
    this.content = content;
  }
  public String getFooter() {
    return footer;
  }
  public void setFooter(String footer) {
    this.footer = footer;
  }
}

Client.java

public class Client {

  public static void main(String[] args) {
    ReportDirector reportDirector = new ReportDirector();
    reportDirector.buildReport();
    Report report = reportDirector.getReport();

    //use report object as per business
    System.out.println(report.getContent());
  }
}


And there you are. You've seen the builder pattern used to help create complex objects, but whose user inputs aren't all required. Now that you've seen it in action, go out and grab some Subway.

Builder pattern

Published at DZone with permission of Arun Pandey, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 12 Modern CSS Techniques For Older CSS Problems
  • Automation Testing vs. Manual Testing: What's the Difference?
  • How Java Apps Litter Beyond the Heap
  • What I Miss in Java, the Perspective of a Kotlin Developer

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