A Look at the Builder Pattern

DZone 's Guide to

A Look at the Builder Pattern

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

· Java Zone ·
Free Resource

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.


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


public class MyBuilder implements IBuilder{
  private Report report;

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

  public Report getReport(){   
    return report;

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

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

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

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

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


public class ReportDirector {

  private IBuilder myBuilder;

  public ReportDirector(){
    myBuilder = new MyBuilder();
  public void buildReport(){
    // report building logic/steps

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


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;


public class Client {

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

    //use report object as per business

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.

java ,design pattern ,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.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}