Video in progress

When a constructor gets too many parameters, how does the Builder pattern help?

A Builder lets you assemble an object step by step through named methods. Instead of a confusing 8-parameter constructor, you get code that's easy to read and hard to get wrong.

📖 Answer

The problem looks like this:

new Notification("Salom", "ali@mail.uz", null, true, false, 3, null, "uz");

Which true means what? If you swap two Strings, the compiler says nothing. This is called the "telescoping constructor" problem.

With a Builder:

Notification n = Notification.builder()
        .subject("Salom")
        .to("ali@mail.uz")
        .urgent(true)
        .retries(3)
        .lang("uz")
        .build();

Every value is given by name, and you can skip the optional fields. In build() you can validate and return a finished, immutable object.

When you don't need it: for a plain DTO with 2–3 fields a Builder is overkill. In Java 16+ a record is often enough for that.