Builder

Beginner · 15 min read · Low-Level Design, Creational Patterns

Constructing complex, valid, immutable objects step by step.

The idea in plain words

Some objects have a few required fields and many optional ones. A constructor with nine arguments is hard to read (new Pizza("L", true, false, null, 2), what is false?). A Builder lets you set only what you need, each by name, and then call build().

The problem: telescoping constructors

An HTTP request has a URL and method (required) plus headers, query parameters, body, timeout, retry count and authentication (optional). Constructors don't scale:

The solution: a fluent builder

What you get:

The classic GoF form: director + builders

The original GoF Builder addresses a different emphasis: the same construction process producing different representations.

Where you've seen it

Variations and pitfalls

Pros and cons

Related patterns

Interview questions

  • Why use a Builder instead of setters?
  • What's the role of the Director in the GoF Builder?
  • How do you enforce required parameters in a builder?

Further reading

Key takeaways

  • Builder solves telescoping constructors and half-built mutable objects.
  • Required fields in the builder constructor, optional fields as fluent methods, validation in build(), immutable product.
  • The GoF form adds a director to run the same steps against different builders.
  • Use it when objects have many optional parts; skip it for small objects.

Previous: Factory Method & Abstract Factory · Next: Prototype