What is the Builder design pattern?
The Builder pattern separates the construction of a complex object from its representation, allowing the same construction process to produce different configurations. It is most valuable when an object requires many parameters, some of which are optional, because the alternative — telescoping constructors or large constructor parameter lists — is unreadable and error-prone.
The canonical Java implementation uses a public static nested Builder class. The outer object's constructor is private and takes only the Builder. Client code chains setter-style methods on the Builder, then calls build() to get the final object. Because each setter returns the Builder itself, the methods chain fluently. The resulting outer object is typically immutable — all its fields are final and set only in the private constructor.
Compared to JavaBeans setters, Builder provides immutability (the object can't be partially constructed in a visible state) and thread-safety (no setters means no mutation after construction). Compared to a constructor with many parameters, Builder is self-documenting — .timeout(5000).retries(3) communicates intent that new Connection(5000, 3) does not.
Java standard library examples: StringBuilder is a mutable builder that produces an immutable String. ProcessBuilder constructs a Process. In modern Java, Locale.Builder, HttpRequest.Builder (java.net.http), and Stream.Builder follow this pattern.
Lombok's @Builder annotation generates the nested builder class at compile time, eliminating boilerplate. This is widely used in production codebases.
The Director variant (from GoF) adds a Director class that encapsulates common construction sequences, but this is rarely used in Java — most codebases let callers drive the builder directly.
Builder is appropriate when objects have more than 3-4 optional parameters or when construction involves validation that is cleanest to perform once in build() rather than across multiple setters.
Explains the telescoping constructor problem Builder solves, describes the static nested Builder with fluent chaining, and notes that the result is typically immutable.
Compares to JavaBeans, names JDK examples (ProcessBuilder, HttpRequest.Builder, StringBuilder), mentions Lombok @Builder, and explains when to choose Builder over a factory or constructor.
Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.
Get Graded — Free Assessment