Shivam Chauhan
about 2 months ago
The Builder Design Pattern is a creational pattern that simplifies the construction of complex objects step by step. It separates the construction process from the representation, allowing you to create different types of objects using the same construction code.
In this blog, we'll explore:
The Builder Pattern helps construct complex objects by separating the construction logic from the object itself. This allows the same construction process to create different representations of the object.
Use the Builder Pattern when:
Let’s implement a House Builder where the final product (House) can have optional parts like a swimming pool, garage, and garden.
java// Product
class House {
private String foundation;
private String structure;
private String roof;
private boolean hasGarage;
private boolean hasSwimmingPool;
private boolean hasGarden;
// Getters and toString for displaying house details
public String toString() {
return "House [foundation=" + foundation + ", structure=" + structure + ", roof=" + roof +
", hasGarage=" + hasGarage + ", hasSwimmingPool=" + hasSwimmingPool +
", hasGarden=" + hasGarden + "]";
}
// Builder Class
public static class Builder {
private String foundation;
private String structure;
private String roof;
private boolean hasGarage;
private boolean hasSwimmingPool;
private boolean hasGarden;
public Builder setFoundation(String foundation) {
this.foundation = foundation;
return this;
}
public Builder setStructure(String structure) {
this.structure = structure;
return this;
}
public Builder setRoof(String roof) {
this.roof = roof;
return this;
}
public Builder setGarage(boolean hasGarage) {
this.hasGarage = hasGarage;
return this;
}
public Builder setSwimmingPool(boolean hasSwimmingPool) {
this.hasSwimmingPool = hasSwimmingPool;
return this;
}
public Builder setGarden(boolean hasGarden) {
this.hasGarden = hasGarden;
return this;
}
public House build() {
House house = new House();
house.foundation = this.foundation;
house.structure = this.structure;
house.roof = this.roof;
house.hasGarage = this.hasGarage;
house.hasSwimmingPool = this.hasSwimmingPool;
house.hasGarden = this.hasGarden;
return house;
}
}
}
// Client Code
public class Client {
public static void main(String[] args) {
// Build a luxury house
House luxuryHouse = new House.Builder()
.setFoundation("Concrete")
.setStructure("Steel")
.setRoof("Glass")
.setGarage(true)
.setSwimmingPool(true)
.setGarden(true)
.build();
System.out.println(luxuryHouse);
// Build a basic house
House basicHouse = new House.Builder()
.setFoundation("Wood")
.setStructure("Brick")
.setRoof("Tiles")
.build();
System.out.println(basicHouse);
}
}
plaintextHouse [foundation=Concrete, structure=Steel, roof=Glass, hasGarage=true, hasSwimmingPool=true, hasGarden=true] House [foundation=Wood, structure=Brick, roof=Tiles, hasGarage=false, hasSwimmingPool=false, hasGarden=false]
Here’s the UML diagram for the House Builder example:
The Builder Design Pattern is an essential tool for constructing complex objects in a clean and maintainable way. It shines in scenarios where objects have numerous optional parameters, providing both flexibility and clarity.
By mastering the Builder Pattern, you'll write cleaner, more extensible code for real-world applications. Start incorporating this pattern in your projects to simplify object construction and improve maintainability.