DEV Community

Dev Cookies
Dev Cookies

Posted on

πŸ“ Day Two: Builder Design Pattern in Java

πŸ” 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); } } } 
Enter fullscreen mode Exit fullscreen mode

πŸ’» 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); } } 
Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ Output:

Burger [bun=Sesame, patty=Beef, cheese=true, lettuce=true, tomato=false, mayo=true] 
Enter fullscreen mode Exit fullscreen mode

βš™οΈ UML Diagram (Text Format)

 +-------------------+ | Burger | +-------------------+ | -bun | | -patty | | -cheese | | -lettuce | | -tomato | | -mayo | +-------------------+ | +toString() | +-------------------+ ^ | +----------------------+ | BurgerBuilder | +----------------------+ | +addCheese() | | +addLettuce() | | +addTomato() | | +addMayo() | | +build() | +----------------------+ 
Enter fullscreen mode Exit fullscreen mode

πŸ§‘β€πŸ« 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)