Builder
A builder is an object that collects construction steps and then creates the product. It replaces a constructor with ten parameters and makes optional fields obvious.
Detailed Explanation
Telescoping constructors (User(name), User(name, email), User(name, email, age), ...) explode. A huge constructor with many nulls is unreadable at the call site. A builder has setters (often fluent return this) and a build() that runs validation once.
In Java, a static nested Builder on the product is the usual shape. Fields on the builder are mutable. build() copies them into a final product. Records plus a builder are common when you want a stable API and many optionals. Lombok @Builder generates this; you should still understand the hand-written form.
Java 8+ can use a consumer: User.create(b -> b.name("Ada").email("a@b")). The builder stays inside the method, so nobody holds a half-built builder. StringBuilder is a builder for strings; ProcessBuilder is one in the JDK.
You reach for a builder when construction has several optional parts or when the product must be immutable and valid. You keep a constructor when there are two or three required arguments. You keep a factory method when there is one common recipe (User.guest()).
The cost of a builder for every type is noise. The cost of a builder that does not validate in build() is an invalid product. The cost of a public builder that is also used as a mutable bean after build() is a broken invariant; build() should copy.
Thread safety: a builder is usually thread-confined. Do not share it. The product should be immutable if you share that.
Required fields belong either on build() validation or on a staged builder (name() returns a type that exposes build()). Optional fields get defaults on the builder. Do not make every field optional and then throw a vague IllegalStateException with no field name. Say which one is missing. Call sites of a good builder read like a form, not like a pile of nulls.
Code Examples
public final class User {
private final String name;
private final String email;
private final int age;
private User(Builder b) {
this.name = b.name;
this.email = b.email;
this.age = b.age;
}
public static Builder builder() {
return new Builder();
}
public static final class Builder {
private String name;
private String email = "";
private int age;
public Builder name(String name) {
this.name = name;
return this;
}
public Builder email(String email) {
this.email = email;
return this;
}
public Builder age(int age) {
this.age = age;
return this;
}
public User build() {
if (name == null || name.isBlank()) {
throw new IllegalStateException("name required");
}
return new User(this);
}
}
public static void main(String[] args) {
User u = User.builder().name("Ada").age(36).build();
System.out.println(u.name); // prints: Ada
}
}Common Gotchas
Validate in `build()`, not only in setters
callers can skip a setter.
A builder for two required fields is ceremony
use a constructor.
Do not mutate the builder after `build` if `build` reused the same objects
copy collections.
Sharing a builder across threads races
Sharing a builder across threads races