Builder
Separate the construction of a complex object from its representation so that the same construction process can create different representations.
The Problem
A constructor with 8+ parameters is hard to read and error-prone. Optional parameters cause telescoping constructors or messy overloads. The order of arguments is non-obvious and parameters of the same type are silently swappable.
Structure
Execution Walkthrough
Define product
Product (SimConfig) has many optional fields with sensible defaults. Direct construction is unreadable.
Builder methods
Chain calls
build()
Reuse builder
Code Comparison
Full C++ Implementation
Participants
Where it is used
SQL query builders
QueryBuilder.select("*").from("events").where("t > 0").limit(100).build()
Protobuf / gRPC
MessageLite::Builder assembles fields before build() produces an immutable, validated message.
HTTP clients
libcurl: set URL, headers, timeout, auth step-by-step before curl_easy_perform().
Simulation config
SimConfigBuilder sets thread count, event limit, sync mode, and verbosity before engine construction.