Builder Pattern
Some objects need a lot of information to create, and most of it is optional. An email needs a recipient and a subject, and may also have a body, CC addresses, attachments, a priority flag and a scheduled send time. A database query needs a table, and may have columns, any number of filters, a sort order and a limit. A constructor that takes all of that is hard to call and easy to call wrongly.
The Builder pattern splits creation into steps. You set each part by name on a separate builder object, in any
order, and then call build(). The builder checks that the parts make sense together and returns the finished
object, which is usually immutable. Nobody ever holds a half-made one.
🧱 In one sentence: collect an object's parts step by step in a separate builder, then let
build()validate them and create the finished, immutable object in one go.
The Problem It Solves
The problem is sharpest in languages without named arguments, so here it is in Java. An Email class grows one
optional field at a time, and each field adds another constructor:
public class Email {
public Email(String to, String subject) {
this(to, subject, "", null, false, 0);
}
public Email(String to, String subject, String body) {
this(to, subject, body, null, false, 0);
}
public Email(String to, String subject, String body, String replyTo) {
this(to, subject, body, replyTo, false, 0);
}
public Email(String to, String subject, String body, String replyTo,
boolean highPriority, long sendAt) { /* assign fields */ }
}
Email e = new Email("[email protected]", "Invoice", "Hi Ana", null, true, 0);
This is called the telescoping constructor, and it has four problems.
The call site is unreadable. What do null, true and 0 mean? You have to open the class to find out, and
so does every reviewer.
Arguments of the same type swap silently. to, subject, body and replyTo are all strings. Swap two of
them and the code compiles, and the customer gets an email whose subject is their own address.
Combinations explode. To set sendAt without a reply-to address you still pass null for it. Six optional
fields would need dozens of constructors to cover every combination, so nobody writes them, and callers pass
placeholders instead.
Lists don't fit. CC addresses and attachments are lists that callers naturally add one at a time. A constructor wants them all at once.
The usual escape is an empty constructor plus setters. It is worse:
Email e = new Email();
e.setTo("[email protected]");
e.setSubject("Invoice");
mailer.send(e); // forgot the body, and nothing noticed
e.setTo("[email protected]"); // and anyone can change it after sending
Now the object is mutable, it can be used half-built, and there is no single place to check rules like "a recipient is required" or "the recipient must not also be on CC". Sharing it between threads is unsafe because anyone can change it.
💡 Python note: keyword arguments with defaults and a frozen dataclass solve most of this in Python:
Email(to="[email protected]", subject="Invoice", high_priority=True)is readable, can't swap silently, and skips any optional field. That is usually the right answer in Python. A builder earns its place there when parts are added step by step (a filter at a time, underifs) or when the rules span several fields.