In this tutorial I am going to explain about builder design pattern, this is one of the most widely used design pattern in Java. Builder design pattern comes under the creational design pattern since builder design pattern helps in creating an object using step-by-step approach.
Subscribe to our future updates.
Builder design pattern solves the problem of creating an object with lot of parameters and where some of the parameters are mandatory and some are optional. The builder design pattern is very much similar to abstract factory design pattern. The key difference between abstract factory pattern and builder pattern is that, where in abstract factory pattern factory methods are creating the objects. But in builder pattern, clear instructions are given to the builder to with step-by-step approach for creating the objects. That means you have complete control on what to be included and excluded in the object creation.
Builder Design Pattern Example
Here is a very simple example that demonstrates how to use builder design pattern for creating the objects. In this builder design pattern example, I am creating a Family
object using the FamilyBuilder
. Here if you notice, FamilyBuilder
class is static class with each method adding one propery to the object. Finally, a build
that completes the object creation. I hope that this example helps you understand how to use builder design pattern. If you have any questions, please write it in the comments section.
Family.java
package net.javabeat.designpatterns.builderpattern; import java.util.List; public class Family { private String familyName; private String husband; private String wife; private int numberOfKids; private List<String> kidsNames; private int annualIncome; private boolean isCarOwned; private boolean isHouseOwned; private String city; private String country; private Family(FamilyBuilder builder){ this.familyName = builder.familyName; this.husband = builder.husband; this.wife = builder.wife; this.numberOfKids = builder.numberOfKids; this.kidsNames = builder.kidsNames; this.annualIncome = builder.annualIncome; this.isCarOwned = builder.isCarOwned; this.isHouseOwned = builder.isHouseOwned; this.city = builder.city; this.country = builder.country; } //All getter, and NO setter to provide immutability public String getFamilyName() { return familyName; } public String getHusband() { return husband; } public String getWife() { return wife; } public int getNumberOfKids() { return numberOfKids; } public List<String> getKidsNames() { return kidsNames; } public int getAnnualIncome() { return annualIncome; } public boolean isCarOwned() { return isCarOwned; } public boolean isHouseOwned() { return isHouseOwned; } public String getCity() { return city; } public String getCountry() { return country; } public String toString(){ StringBuilder result = new StringBuilder(); result.append("Family Name : "+this.familyName); result.append("\nHusband Name : "+this.husband); result.append("\nWife Name : "+this.wife); result.append("\nNumber of Kids : "+this.numberOfKids); result.append("\nKids Names : "+this.kidsNames); result.append("\nAnnual Income : "+this.annualIncome); result.append("\nIs Own Car : "+this.isCarOwned); result.append("\nIs Own House : "+this.isHouseOwned); result.append("\nCity : "+this.city); result.append("\nCountry : "+this.country); return result.toString(); } public static class FamilyBuilder{ private String familyName; private String husband; private String wife; private int numberOfKids; private List<String> kidsNames; private int annualIncome; private boolean isCarOwned; private boolean isHouseOwned; private String city; private String country; public FamilyBuilder(String familyName){ this.familyName = familyName; } FamilyBuilder husband(String husband){ this.husband = husband; return this; } FamilyBuilder wife(String wife){ this.wife = wife; return this; } FamilyBuilder kids(int kids){ this.numberOfKids = kids; return this; } FamilyBuilder kidsNames(List kidsNames){ this.kidsNames = kidsNames; return this; } FamilyBuilder annualIncome(int annualIncome){ this.annualIncome = annualIncome; return this; } FamilyBuilder isCarOwned(boolean isCarOwned){ this.isCarOwned = isCarOwned; return this; } FamilyBuilder isHouseOwned(boolean isHouseOwned){ this.isHouseOwned = isHouseOwned; return this; } FamilyBuilder city(String city){ this.city = city; return this; } FamilyBuilder country(String country){ this.country = country; return this; } //Return the finally construcuted Family object public Family build() { Family family = new Family(this); return family; } } }
BuilderDesignPatternExample.java
package net.javabeat.designpatterns.builderpattern; import java.util.ArrayList; import java.util.List; public class BuilderDesignPatternExample { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("Nancy"); Family family = new Family.FamilyBuilder("Happy Family").husband("John").wife("Marry"). kids(1).kidsNames(list).annualIncome(100000).isCarOwned(true). isHouseOwned(true).city("NewYork").country("USA").build(); System.out.println(family); } }