Builder Design Pattern
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 ?
- When the Object Has Many Optional Parameters.
- we want to validate attribute before creating Object.
- 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.
in Above code we have some Problem
- client may forgot to add mandatory Attributes.
- setter method called after creation of object so cannot check validation before creation of object.
- multiple setter is called so not readable and maintainable.
Idea 2: Using Constructor:-
we can create All args constructor and pass value from constructor.
Above code has several error
- order should be followed
- optional parameter we need to pass null
- if n optional parameter then we need to create 2^n constructor
- difficult to read
- difficult to extend
- 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.
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.
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.
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.
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
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.
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.
Real Life Example Of Builder design Pattern:-
Here are real-life examples where the Builder pattern is used:
- StringBuilder (Java)
- StringBuffer (Java)
- StringBuilder (C#)
- DocumentBuilder (XML Parsers in Java)
- HttpClient.Builder (Java 11+ HttpClient API)
- Toast Builder (Android development)
- AlertDialog.Builder (Android development)
- Lombok @Builder (Java Lombok Library)
- Retrofit.Builder (Android and Java API Client)
- OkHttpClient.Builder (HTTP Client in Java)





![System Design Interview Series:3/30[Back-of-Envelope Estimation]](https://miro.medium.com/v2/resize:fit:1358/format:webp/1*xgLXa5l3deDFviuJtxrtqA.png)

![[LLD-8] State Design Pattern: Vending Machine Low-Level Design](https://miro.medium.com/v2/resize:fit:1358/format:webp/1*jap_cRszSOiPk-lu65mFLw.png)