π What is the Builder Design Pattern?
The Builder Pattern is a creational design pattern used to construct complex objects step by step. Unlike constructors with numerous parameters, the builder pattern makes object creation more readable, flexible, and maintainable.
β When Should You Use It?
- When an object requires many parameters to construct (especially optional ones).
- To prevent a telescoping constructor (multiple constructors with different parameter combinations).
- To make the code more readable and robust.
π§ Real-World Analogy
Think of building a burger at a fast-food place. You choose bun type, patties, cheese, sauces, toppings, etc. A BurgerBuilder helps you step-by-step and finally gives you the complete burger π.
π§± Structure
// 1. Product Class public class Burger { private String bun; private String patty; private boolean cheese; private boolean lettuce; private boolean tomato; private boolean mayo; // Private constructor, only accessible via Builder private Burger(BurgerBuilder builder) { this.bun = builder.bun; this.patty = builder.patty; this.cheese = builder.cheese; this.lettuce = builder.lettuce; this.tomato = builder.tomato; this.mayo = builder.mayo; } @Override public String toString() { return "Burger [bun=" + bun + ", patty=" + patty + ", cheese=" + cheese + ", lettuce=" + lettuce + ", tomato=" + tomato + ", mayo=" + mayo + "]"; } // 2. Builder Class (Static Nested Class) public static class BurgerBuilder { private String bun; private String patty; private boolean cheese; private boolean lettuce; private boolean tomato; private boolean mayo; public BurgerBuilder(String bun, String patty) { this.bun = bun; this.patty = patty; } public BurgerBuilder addCheese(boolean cheese) { this.cheese = cheese; return this; } public BurgerBuilder addLettuce(boolean lettuce) { this.lettuce = lettuce; return this; } public BurgerBuilder addTomato(boolean tomato) { this.tomato = tomato; return this; } public BurgerBuilder addMayo(boolean mayo) { this.mayo = mayo; return this; } public Burger build() { return new Burger(this); } } }
π» Client Code
public class BuilderPatternDemo { public static void main(String[] args) { Burger burger = new Burger.BurgerBuilder("Sesame", "Beef") .addCheese(true) .addLettuce(true) .addTomato(false) .addMayo(true) .build(); System.out.println(burger); } }
π§ͺ Output:
Burger [bun=Sesame, patty=Beef, cheese=true, lettuce=true, tomato=false, mayo=true]
βοΈ UML Diagram (Text Format)
+-------------------+ | Burger | +-------------------+ | -bun | | -patty | | -cheese | | -lettuce | | -tomato | | -mayo | +-------------------+ | +toString() | +-------------------+ ^ | +----------------------+ | BurgerBuilder | +----------------------+ | +addCheese() | | +addLettuce() | | +addTomato() | | +addMayo() | | +build() | +----------------------+
π§βπ« Key Takeaways
- Makes object creation readable and avoids messy constructor overloads.
- Supports immutability and encapsulation.
- Flexible and fluent for creating different combinations.
π£ Next Up: Want to explore Singleton, Strategy, or Decorator pattern on Day 3?
Let me know your choice and Iβll keep the same clean format with code, diagrams, and examples!
Top comments (0)