Interview question
What is the Builder design pattern and how does it help with complex object construction? Builder design pattern क्या है और complex object construction में कैसे मदद करता है?
Answer
The Builder pattern constructs complex objects step by step, avoiding constructors with many parameters (telescoping constructors) and making object creation more readable.
// PROBLEM - telescoping constructor anti-pattern
class PizzaOld {
PizzaOld(String size, boolean cheese, boolean pepperoni, boolean mushroom, boolean olives) {
// hard to remember parameter order, easy to make mistakes
}
}
new PizzaOld('Large', true, false, true, false); // what do these booleans mean?!
// SOLUTION - Builder pattern
class Pizza {
private final String size; // required
private final boolean cheese;
private final boolean pepperoni;
private final boolean mushroom;
private Pizza(Builder builder) {
this.size = builder.size;
this.cheese = builder.cheese;
this.pepperoni = builder.pepperoni;
this.mushroom = builder.mushroom;
}
public static class Builder {
private final String size; // required, set in constructor
private boolean cheese = false;
private boolean pepperoni = false;
private boolean mushroom = false;
public Builder(String size) {
this.size = size;
}
public Builder cheese(boolean value) { this.cheese = value; return this; }
public Builder pepperoni(boolean value) { this.pepperoni = value; return this; }
public Builder mushroom(boolean value) { this.mushroom = value; return this; }
public Pizza build() {
return new Pizza(this);
}
}
}
// Readable, self-documenting object creation with method chaining
Pizza pizza = new Pizza.Builder('Large')
.cheese(true)
.mushroom(true)
.build();
// Only sets what's needed, order doesn't matter, clear what each value means
// Real-world Java examples: StringBuilder, StringBuffer,
// java.time.LocalDate (via factory+builder-like chains),
// Stream.Builder, Lombok's @Builder annotationBuilder pattern complex objects को step by step बनाता है, many-parameter constructors (telescoping constructors) से बचाता है, object creation को ज़्यादा readable बनाता है।
// Problem - telescoping constructor
class PizzaOld {
PizzaOld(String size, boolean cheese, boolean pepperoni, boolean mushroom, boolean olives) {
}
}
new PizzaOld('Large', true, false, true, false); // ये booleans क्या मतलब रखते हैं?!
// Solution - Builder pattern
class Pizza {
private final String size;
private final boolean cheese;
private final boolean pepperoni;
private final boolean mushroom;
private Pizza(Builder builder) {
this.size = builder.size;
this.cheese = builder.cheese;
this.pepperoni = builder.pepperoni;
this.mushroom = builder.mushroom;
}
public static class Builder {
private final String size;
private boolean cheese = false;
private boolean pepperoni = false;
private boolean mushroom = false;
public Builder(String size) {
this.size = size;
}
public Builder cheese(boolean value) { this.cheese = value; return this; }
public Builder pepperoni(boolean value) { this.pepperoni = value; return this; }
public Builder mushroom(boolean value) { this.mushroom = value; return this; }
public Pizza build() {
return new Pizza(this);
}
}
}
Pizza pizza = new Pizza.Builder('Large')
.cheese(true)
.mushroom(true)
.build();
// Real-world उदाहरण: StringBuilder, Lombok का @BuilderWas this answer clear?