Skip to content
BENIBIX Learn

Builder Pattern

Development🏗️ ArchitectureDesign Patterns

Also known as Builder Design Pattern, Fluent Builder

Making a big object one step at a time. At the end you call build.

Think of it like

It is like ordering a sandwich step by step. Bread, then filling, then sauce, then wrap it up.

Example

Lucy's code makes web requests with a builder. She sets the address, adds a header and a timeout, then calls build.

Why it matters

Code that makes objects reads like a list of choices. Bad mixes are caught in one place, in build.

Definition

The builder pattern makes a complex object step by step instead of all at once. A builder object has one method for each part you might set. You call the ones you need, often chained in one line, then call build. The builder checks the parts and hands back the finished object. It replaces long constructors with many arguments that are easy to mix up.

Example code

javascript
const request = new RequestBuilder()
  .url("https://example.com/api/orders")
  .header("Accept", "application/json")
  .timeout(5000)
  .build();
Each line sets one part. build() checks them and makes the request.
Builder, top right: set each part in turn, then build makes the finished object.

Common mistake

Using a builder for a small object with two fields. A plain constructor is shorter and just as clear.

Key takeaways

  1. 1
    Step by step. One part at a time.
  2. 2
    Build at the end. Check, then make.
  3. 3
    No long argument lists. Named steps instead.