Builder
Builder assembles a complex value through named construction steps. The finished product should not accidentally remain coupled to mutable construction state.
What you’ll understand
- Builder assembles a complex value through named construction steps. The finished product should not accidentally remain coupled to mutable construction state.
- Build copies the current header collection into a separate product before subsequent builder mutations.
- Evaluate the tradeoff: A builder adds an intermediate object and can obscure required fields if validation is delayed. For a small value with a few obvious arguments, a constructor or named options object is simpler.
Useful first: Classes and Objects
The idea and when to recognize it
Builder assembles a complex value through named construction steps. The finished product should not accidentally remain coupled to mutable construction state.
- Requirement: A request builder accumulates headers and produces a request snapshot.
- Change to anticipate: Reuse the builder after the first build without modifying the already-produced request.
- Design goal: Build copies the current header collection into a separate product before subsequent builder mutations.
Start with the concrete problem
A request builder accumulates headers and produces a request snapshot.
Reuse the builder after the first build without modifying the already-produced request.
| Direct implementation | Revised design |
|---|---|
| Returning the builder’s mutable headers directly lets future construction steps rewrite earlier requests. | Build copies the current header collection into a separate product before subsequent builder mutations. |
| The baseline case can hide the problem. Compare the extension and boundary fixtures before accepting this design. | A builder adds an intermediate object and can obscure required fields if validation is delayed. For a small value with a few obvious arguments, a constructor or named options object is simpler. |
Responsibilities and contracts
Build copies the current header collection into a separate product before subsequent builder mutations.
- RequestBuilder: Accumulates construction state. Public operations: header(value), build()
- Request: Owns a completed header snapshot. Public operations: headers
Read the design
Select a participant to inspect its responsibility and public operations. Follow the labeled relationships; an arrow indicates direction, while the legend distinguishes ownership, substitution, and temporary use.
Preparing the design walkthrough…
Follow the interaction
Choose a supplied workload and compare the direct and revised implementations. Step through the same operation in both languages. Object values are recorded after each highlighted statement; previous steps remain unchanged.
Preparing the design walkthrough…
Implementation and expected output
Read the complete example alongside its input and expected output. The implementation selector changes the displayed program; the language selector preserves your current scenario and playback position.
Preparing the design walkthrough…
Try the idea in another setting
A report builder can assemble sections and return an immutable document snapshot.
- Name the new client contract before adapting the example.
- Identify which state belongs to each object and who controls its lifetime.
- Write one normal case and one boundary case before extending the design.
Tradeoffs and interview takeaways
A builder adds an intermediate object and can obscure required fields if validation is delayed. For a small value with a few obvious arguments, a constructor or named options object is simpler.
Common mistake: Returning the builder’s mutable headers directly lets future construction steps rewrite earlier requests.
- Explain the choice: Build copies the current header collection into a separate product before subsequent builder mutations.
- Challenge your choice: A builder adds an intermediate object and can obscure required fields if validation is delayed. For a small value with a few obvious arguments, a constructor or named options object is simpler.
- Describe a requirement that would make the simpler implementation sufficient.
Design challenge and knowledge check
Preparing the design walkthrough…