Builder pattern
Строитель
This object-creation pattern lets you assemble a complex object one step at a time. You supply settings or parts to a separate builder, then ask it for the finished object.
Also known as: builder
What problem does it solve?
Suppose you're creating a report with a required title, an optional row limit, and several display settings. With a constructor call such as new Report("Sales", true, false, 20), you have to look up the signature to know what the flags and number mean. A builder gives those choices names, keeps track of defaults, and holds assembly state until the caller asks for the finished report.
How it works
A builder exposes operations for supplying values or assembling parts. Required inputs can go in the builder's constructor, while optional inputs get their own methods. Those methods often return the builder so calls can be chained, but chaining is not required. The final operation, typically called build(), creates the product and can reject missing inputs or incompatible settings.
The classic Gang of Four pattern also covers a different kind of variation: the same construction steps can produce different representations. A sequence such as adding a heading, a table, and a summary could produce either HTML or PDF, depending on which concrete builder performs the steps. A separate director can own that sequence. Many everyday builders produce just one type, and the caller chooses the steps directly.
Example in Java
record Report(String title, int maxRows) {}
class ReportBuilder {
private final String title;
private int maxRows = 100;
ReportBuilder(String title) {
if (title == null || title.isBlank()) {
throw new IllegalArgumentException("Title is required");
}
this.title = title;
}
ReportBuilder maxRows(int value) {
maxRows = value;
return this;
}
Report build() {
if (maxRows < 1) {
throw new IllegalArgumentException("maxRows must be positive");
}
return new Report(title, maxRows);
}
}
// Example call:
// var report = new ReportBuilder("Sales").maxRows(20).build();
The row limit defaults to 100 when the caller skips maxRows(). Each call to build() creates a new report from the builder's current settings. Here, calling the Report constructor directly bypasses validation. If every report must satisfy those rules, enforce them in the product's constructor or prevent callers from bypassing the builder.
When to use it
Builders are useful for client configuration, query objects, and test fixtures with many optional settings. They're less useful when a short constructor already tells the whole story. Named arguments and defaults can solve the readability problem without adding another class. Also, a fluent API isn't necessarily a builder: methods can return this simply to chain updates on an object that is already in use.
What interviewers ask
A common comparison is Builder versus Factory. A factory normally returns a product in one call and hides the choice of implementation or creation logic; a builder exposes construction as a series of decisions or steps. Be ready to explain what happens if build() is called twice or the builder is changed afterward. If the result is meant to be immutable, it shouldn't share mutable collections with the builder.