Sitemap
SIDDHARTH SHARMA

SIDDHARTH SHARMA

Builder Design Pattern

4 min readJan 21, 2025

The Builder Design Pattern is a creational design pattern that separates the construction of a complex object from its representation. This allows the same construction process to create different representations of an object. It provides a step-by-step approach to construct an object while maintaining immutability and hiding the details of construction logic.

In simpler terms, it allows you to build complex objects piece by piece, ensuring better readability, maintainability, and control over the construction process.

when to use Builder design Pattern ?

  1. When the Object Has Many Optional Parameters.
  2. we want to validate attribute before creating Object.
  3. When Object Creation Requires Several Steps.

Let’s Suppose we need to create An object of Student Class that have lot of Attributes like Name,id,psp,universityName,Age,phoneNumber,batch,gradYear.Also before creation the object validate that Age should be positive then only create an object.

Idea 1: Using Setter Function:-

we will create getter and setter and then create the object using setter method.

Press enter or click to view image in full size

in Above code we have some Problem

  1. client may forgot to add mandatory Attributes.
  2. setter method called after creation of object so cannot check validation before creation of object.
  3. multiple setter is called so not readable and maintainable.

Idea 2: Using Constructor:-

we can create All args constructor and pass value from constructor.

Press enter or click to view image in full size

Above code has several error

  1. order should be followed
  2. optional parameter we need to pass null
  3. if n optional parameter then we need to create 2^n constructor
  4. difficult to read
  5. difficult to extend
  6. error prone (pass university name instead name as signature is same)

7. Also Constructor overloading not possible as some parameter signature can be same like

Student(String UniversityName){}
Studnet(String Name){}

Idea 3:- Map data structure

So we need some sort of data structure that can store key values and we can pass this data structure into the constructor.

Press enter or click to view image in full size

Above code has several issue
1. need to remember all keys Name
2. no type check on value I can pass age string also
3.user can put wrong key like instead age can pass AGEE

Idea 4:- Builder Class

Get SIDDHARTH SHARMA’s stories in your inbox

Join Medium for free to get updates from this writer.

we need something like map so we will use another class StudentBuilder which will have same attribute as Student and then we will use validation in Student object.

Press enter or click to view image in full size

Above code has some issue we cannot say that StudentBuilder is somehow interrelated to Student Class so we need to make code more understandable.

Idea 5:- Make a Builder Static method in Student:-

we can create a static method in the student class which will create the object of builder.

Press enter or click to view image in full size

In the above code, StudentBuilder is Acting Like a container but it is not actually building Student.

Idea 6:-Create Build method inside builder

Create Build method inside Builder class and give Responsblity to create validation and create Student.

Press enter or click to view image in full size

Now will this code make more sense or not first student create builder then builder store intermediate result then builder call build method and validate and return student object.

Now Make Core More Clear

Press enter or click to view image in full size

So Now which Code is More Understandable and readable in second method we need to store intermediate state of builder and at last we will call build so we need to change StudentBuilder setter return type to StudentBuilder.

Press enter or click to view image in full size

Now Above code have one more Issue what if I client call Constructor of Student directly.and set age=-1

Student s=new Student();
s.setage(-1);

so we need to make constructor of student Private But we have couple of issue
1. StudentBuilder build method also cannot access constructor

2.we need to create studentBuilder class inside Student

3. and builder method is static which is creating stringbuilder so only static method can access only static member so builder class should be static.

Press enter or click to view image in full size

Real Life Example Of Builder design Pattern:-
Here are real-life examples where the Builder pattern is used:

  1. StringBuilder (Java)
  2. StringBuffer (Java)
  3. StringBuilder (C#)
  4. DocumentBuilder (XML Parsers in Java)
  5. HttpClient.Builder (Java 11+ HttpClient API)
  6. Toast Builder (Android development)
  7. AlertDialog.Builder (Android development)
  8. Lombok @Builder (Java Lombok Library)
  9. Retrofit.Builder (Android and Java API Client)
  10. OkHttpClient.Builder (HTTP Client in Java)

--

--