The Builder pattern constructs a complex object step by step, separating the construction logic from the object's final representation. The same building process can produce different configurations of the object depending on which steps are called.
Key Points: • Useful when an object has many optional or interdependent fields that would otherwise require telescoping constructors. • Construction steps can be called in a controlled sequence, improving readability. • The same builder can produce different representations by varying which steps run. • It keeps the target class immutable, since fields are only set during the build process. • Often paired with a fluent, chainable API for readability.
Example: Building a Pizza object with optional toppings, size, and crust type is awkward with a constructor that takes ten parameters, but a PizzaBuilder lets you call only the setters you need, like builder.size(LARGE).topping(CHEESE).build().
Interview Tip: A concise interview answer is:
"I use the Builder pattern when an object has many optional parameters or a multi-step construction process, since it avoids telescoping constructors and lets me build the object piece by piece with readable, chainable calls while keeping the final object immutable."