Creational Patterns  «Prev 

Builder Pattern in Java

Guidelines for using the Builder Design Pattern in Java
  1. Make a static nested class called Builder inside the class whose object will be created by Builder. In this example its Cake.
  2. The Builder class will have exactly the same set of fields as the original class.
  3. The Builder class will expose a method for adding ingredients (for example, by means of the sugar() method). Each method will return the same Builder object. Builder will be enriched with each method call.
  4. The method will copy all Builder field values into the actual class and return an object of Item class.
  5. Item class (the class for which we are creating Builder) should have a private constructor to create its object from the build() method and prevent outsiders from accessing its constructor.


public class BuilderPatternExample {   
 public static void main(String args[]) {       
   //Creating object using Builder pattern in java
   Cake whiteCake = new Cake.Builder().sugar(1).butter(0.5).
   eggs(2).vanila(2).flour(1.5). bakingpowder(0.75).milk(0.5).build();       
   //Cake is ready to eat :)

class Cake {
 private final double sugar;   //cup
 private final double butter;  //cup
 private final int eggs;
 private final int vanila;     //spoon
 private final double flour;   //cup
 private final double bakingpowder; //spoon
 private final double milk;  //cup
 private final int cherry;

 public static class Builder {
 private double sugar;   //cup
 private double butter;  //cup
 private int eggs;
 private int vanila;     //spoon
 private double flour;   //cup
 private double bakingpowder; //spoon
 private double milk;  //cup
 private int cherry;

 //builder methods for setting property
 public Builder sugar(double cup){this.sugar = cup; return this; }
 public Builder butter(double cup){this.butter = cup; return this; }
 public Builder eggs(int number){this.eggs = number; return this; }
 public Builder vanila(int spoon){this.vanila = spoon; return this; }
 public Builder flour(double cup){this.flour = cup; return this; }
 public Builder bakingpowder(double spoon){this.sugar = spoon; return this; }
 public Builder milk(double cup){this.milk = cup; return this; }
 public Builder cherry(int number){this.cherry = number; return this; }
 //return fully build object
 public Cake build() {
  return new Cake(this);

//private constructor to enforce object creation through builder
private Cake(Builder builder) {
 this.sugar = builder.sugar;
 this.butter = builder.butter;
 this.eggs = builder.eggs;
 this.vanila = builder.vanila;
 this.flour = builder.flour;
 this.bakingpowder = builder.bakingpowder;
 this.milk = builder.milk;
 this.cherry = builder.cherry;       

public String toString() {
  return "Cake{" + "sugar=" + sugar + ", butter=" + butter + ", eggs=" + eggs + ", 
  vanila=" + vanila +  ", flour=" + flour + ", bakingpowder=" + bakingpowder + ", 
  milk=" + milk + ", cherry=" + 
  cherry + '}';

Cake{sugar=0.75, butter=0.5, eggs=2, vanila=2, flour=1.5, bakingpowder=0.0, milk=0.5, cherry=0}

Advantages of the Builder Design Pattern


  1. The code is more maintainable if the number of fields required to create an object is greater than 4 or 5.
  2. The code is less likely to cause errors because the user will know what is being passed because of the explicit method call.
  3. The code is more robust because only the fully constructed object will be available to the client.

Disadvantages of the Builder Design Pattern

There exists code duplication because the Builder needs to copy all the fields from the Original class.

When to use Builder Design Pattern in Java

You should use the Builder pattern when: Builder Design Pattern is a creational pattern and should be used when the number of parameters required in a constructor is difficult to manage.