← Back to Home

Builder Pattern

Construct complex objects step-by-step without constructor parameter explosion and confusion.

What is it?

The Builder pattern lets you construct complex objects step-by-step using clear, readable method calls instead of confusing constructors with many parameters.

Interactive Demo: Builder vs Constructor Parameter Explosion

Build a custom computer. See how Builder stays clear while constructor parameters become unmanageable:

🔧 Builder Pattern

Build Process:
Click buttons above to configure computer...

💥 Constructor Parameters

Constructor requires:
CPU, RAM, Storage, GPU, Motherboard, PSU, Cooling, Case
Easy to mess up the order!
Constructor Calls:
Click buttons above to simulate constructor calls...

💡 Key Insight:

  • Builder Pattern: Clear method names, any order, impossible to confuse parameters!
  • Constructor: 8+ parameters in exact order, easy to swap and create bugs!
  • Real Impact: Builder prevents configuration errors, constructor creates maintenance nightmares

Code Example

class ComputerBuilder {
  private computer: Partial<ComputerSpecs> = {};

  setCPU(cpu: string): ComputerBuilder {
    this.computer.cpu = cpu;
    return this; // Method chaining
  }

  setRAM(ram: string): ComputerBuilder {
    this.computer.ram = ram;
    return this;
  }

  setGPU(gpu: string): ComputerBuilder {
    this.computer.gpu = gpu;
    return this;
  }

  build(): Computer {
    return new Computer(this.computer);
  }
}

// Usage - Clear and readable
const computer = new ComputerBuilder()
  .setCPU("Intel i9-13900K")     // Clear what each parameter is
  .setRAM("32GB DDR5")           // Any order you want
  .setGPU("RTX 4090")            // Self-documenting
  .build();

// vs Constructor Approach - Confusing and error-prone
const computer2 = new Computer(
  "Intel i9-13900K",  // What is this parameter?
  "32GB DDR5",        // What order should these be in?
  "1TB SSD",          // Easy to swap these accidentally
  "RTX 4090",         // Which one is GPU vs Storage?
  "Z790 Extreme",     // Did I get the order right?
  "1000W Platinum",   // Getting confusing...
  "AIO Liquid",       // Too many parameters!
  "Gaming ATX"        // Wrong order = Wrong computer!
);

Common Uses

  • Configuration objects (database connections, API clients)
  • Complex UI components with many optional properties
  • SQL query builders and fluent APIs
  • Form builders and validation setup
  • Test data builders and mock object creation
  • HTTP request builders (headers, params, body)

When to Use

  • When constructors would have many parameters (4+ parameters)
  • When you need optional or default values for complex objects
  • When parameter order could cause confusion or bugs
  • When you want to create different variations of the same object
  • When you need to validate or transform data during construction

Caution

  • Adds complexity for simple objects (use regular constructors for 1-3 params)
  • Requires more classes and methods than direct construction
  • May be slower than direct construction (usually negligible)
  • Team needs to understand the pattern to use effectively