The Builder Design Pattern is used to construct complex objects step-by-step.
It separates the construction of an object from its representation, allowing the same construction process to create different representations.
Look out for these patterns in code:
- You see chained calls like:
Product p = new ProductBuilder().setX().setY().build();
- Construction logic is encapsulated in a builder class.
- The main class has many optional fields.
- A Director may manage the building process in some implementations.
- When an object has many optional parameters.
- To avoid telescoping constructors.
- When object construction is complex or conditional.
- To create immutable objects cleanly.
- To improve code readability and maintainability.
Define the class to be built with required fields.
public class Car {
private String make, model, color;
private boolean hasSunroof;
public Car(String make, String model, String color, boolean hasSunroof) {
this.make = make;
this.model = model;
this.color = color;
this.hasSunroof = hasSunroof;
}
}
public class CarBuilder {
private String make, model, color;
private boolean hasSunroof;
public CarBuilder setMake(String make) {
this.make = make;
return this;
}
public CarBuilder setModel(String model) {
this.model = model;
return this;
}
public CarBuilder setColor(String color) {
this.color = color;
return this;
}
public CarBuilder setSunroof(boolean hasSunroof) {
this.hasSunroof = hasSunroof;
return this;
}
public Car build() {
return new Car(make, model, color, hasSunroof);
}
}
Car car = new CarBuilder()
.setMake("Toyota")
.setModel("Camry")
.setColor("Blue")
.setSunroof(true)
.build();
Start from how you want to use it:
Entity e = new EntityBuilder().setX().setY().build();
Then build your Entity
class and EntityBuilder
around it.
- Readable and intuitive object creation.
- Keeps objects immutable post-construction.
- Helps build different versions of the same object.
- Avoids constructor overload hell.
Java API | Builder Used | Purpose |
---|---|---|
StringBuilder |
StringBuilder |
Build strings efficiently |
Stream.Builder |
Stream.builder() |
Build Java Streams |
HttpRequest.Builder |
HttpRequest.Builder |
Create HTTP requests in Java 11+ |
Calendar.Builder |
Calendar.Builder |
Customize calendar fields |
- Builder class:
ClassNameBuilder
- Methods:
setX()
,withX()
,addX()
- Finalizer method:
build()
Builder Pattern helps you construct complex objects in a clean, step-by-step manner — no need to juggle dozens of constructor arguments.
Made with ❤️ by syedyshiraz