intermediate
Builder
Assemble complex objects step by step with validation, defaults, and readable configuration instead of long positional constructors.
Builder assembles complex objects step by step with readable fluency, defaults, and validation before `build()`. It beats telescoping constructors when optional fields combine explosively. In application code, builders often appear for HTTP clients, test fixtures, query objects, and configuration records.
On interviews: compare Builder with constructors, factories, and object literals. Discuss immutability and validation timing.
Common pitfalls: builders that allow invalid partial states; mutable builders shared across threads; duplicating defaults in multiple builder copies.
The trade-off is flexibility versus complexity—know when the simpler path is enough.
Checklist:
- Validate invariants at build time.
- Provide sensible defaults for optional fields.
- Keep builder API discoverable and ordered logically.
- Prefer immutable products after build completes.