☕ Java Advanced

What is the Builder design pattern in Java?

Answer

The Builder pattern separates the construction of a complex object from its representation, allowing the same construction process to create different representations. It is especially useful when an object has many optional parameters — it avoids telescoping constructors and the mutable JavaBean approach. A Builder class has methods for each parameter (returning this for chaining) and a build() method that creates the final immutable object. Example: new Pizza.Builder("large").addTopping("cheese").addTopping("mushroom").build(). Lombok's @Builder annotation generates Builder code automatically. The Builder pattern is used extensively in Java APIs — StringBuilder, ProcessBuilder, and HttpClient.Builder are all examples.