← Back to Java

Builder Design Pattern

JavaMidjava

The Question

What is the Builder design pattern?

What a Strong Answer Covers

  • creational pattern
  • separates construction from representation
  • step-by-step building
  • handles many optional parameters
  • avoids telescoping constructors
  • static inner Builder class
  • build() method
  • immutable objects

Senior-Level Answer

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.

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

Explains the telescoping constructor problem Builder solves, describes the static nested Builder with fluent chaining, and notes that the result is typically immutable.

3/3 — Strong Answer

Compares to JavaBeans, names JDK examples (ProcessBuilder, HttpRequest.Builder, StringBuilder), mentions Lombok @Builder, and explains when to choose Builder over a factory or constructor.

Common Mistakes

  • Confusing StringBuilder with the GoF Builder pattern — StringBuilder is a mutable builder but it doesn't separate construction from representation in the GoF sense.
  • Not mentioning the immutability benefit — treating Builder as just a nicer way to call setters misses the key advantage over JavaBeans.
  • Forgetting that the outer class constructor should be private when using the nested Builder pattern, to force use of the builder.

Follow-Up Questions

  • How would you add validation in a Builder pattern so that build() throws if required fields are missing? — Check required fields in the build() method before constructing the object, throwing IllegalStateException if any required field is null or unset.
  • What problem does Builder solve that a static factory method does not? — Static factories don't scale to many optional parameters. Builder gives named, ordered-independent parameter setting and deferred construction with a single build() call.
  • How does Lombok's @Builder annotation work at the bytecode level? — The Lombok annotation processor generates a nested static Builder class with field setters (returning the builder), an all-args constructor on the outer class, and a build() method — at compile time, before javac sees the source.

Related Questions

  • JVM vs JRE vs JDK
  • Java Platform Independence
  • How JVM Works
  • Main Features of Java
  • public static void main

Can You Explain This Cold?

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
GrindQuestionsAITechnical interview assessment
TermsPrivacyAbout