← Back to PatternPip

BUILDER / THE IDEA IN A MINUTE

Create the parts.
Then the whole.

Builder separates an object’s construction into explicit steps, so callers can assemble the parts they need before asking for the finished object.

The builder owns an unfinished draft. The product is the result. A caller chooses the steps; an optional director can coordinate a reusable sequence.

What changes?
The parts and steps used to construct an object.
What stays stable?
The product’s rules and the named construction operations.
Who chooses the steps?
The caller, or an optional recipe that coordinates them.

THE STRUCTURE / THREE CORE ROLES + AN OPTIONAL DIRECTOR

Choose the steps. Let the builder keep the draft.

The caller asks a ReportBuilder to add parts. The builder owns the unfinished work; build() validates it and returns a separate Report. A reusable recipe can choose the steps for the caller.

Who chooses the construction steps?

Compare direct calls with a reusable recipe.

01 / CALLERChooses the approach

Application setup

buildFooterReport()

The caller creates a fresh ReportBuilder, chooses title, body and footer, then calls build(). No director is used.

Independent report jobs start with fresh builders.

02 / BUILDERSame class

ReportBuilder

Keeps the editable draft and implements the construction steps. For this example, the chosen steps are:

  1. withTitle(…)
  2. withBody(…)
  3. withFooter(…)
  4. build(): Report

Title and body are required. build() checks them before returning a valid product.

03 / PRODUCTSame type

Report

The finished object, separate from the builder. Later draft edits do not change this snapshot.

REPORT / COMPLETED SNAPSHOT

Weekly update

We shipped a new lesson.

Prepared for the team

04 / DIRECTOR ROLEOptional · not used here

weeklyRecipe(…)

Coordinates title, body, summary, chart, footer and build() on a supplied builder. It knows the recipe; ReportBuilder knows how to assemble and validate the report.

This function plays the director role. A separate director class is not required.

The caller supplies a builder. Direct calls or an optional recipe choose its steps. The builder returns the product; the recipe does not store its own draft.

Direct construction: the caller chooses the steps. ReportBuilder validates the draft and returns a Report with a footer.

Try the demo below: assemble a draft, build a report, then edit the draft and compare them.

WATCH IT TAKE SHAPE

Build the parts. Keep the result.

Try it yourself

Add named parts to an unfinished draft. Build a report when it’s ready, then keep editing the draft to see what stays separate.

1 / CHOOSE NAMED STEPS

You choose the parts

Required parts first, or optional ones first. Build checks that the essentials are there.

2 / THE BUILDER

A working draft

0 of 2 required parts ready

BUILDER / CURRENT DRAFT

Title not set

Body not set

Optional parts may be left out.

3 / THE PRODUCT

A finished report

Created only when you call build()

Your finished report will appear here.

Add the required parts, then build.

Start with an empty builder. Add a title and body, then build your report.

Try this: add a title and body, then build. Add a chart or change the title. Your finished report stays the same until you build a new one.

This builder requires a title and body, keeps its draft after building, and returns independent snapshots. Reduced motion is on; changes appear immediately.

Useful when construction has real steps.

Many optional parts, repeated assembly recipes, or several representations built through similar operations can benefit from a dedicated builder.

More machinery is a tradeoff.

A constructor, factory function, options object, or Kotlin named arguments may be enough for simple data. Fluent method chaining alone does not make a useful Builder.

Choose your next step.