Java Omnibus
OOAD & Architecture / GoF patterns / Creational / Builder Pattern in Java
GoF Creational Java Order Management
GoF Pattern

Builder is most useful when object construction is multi-step, optional, or too awkward for constructors alone to express clearly.

Separate the construction of a complex object from its representation so the same construction process can create different representations or configurations.

Intent

What the pattern is trying to do

Separate the construction of a complex object from its representation so the same construction process can create different representations or configurations.

Problem

What force creates the need

Constructors become unreadable or error-prone when objects have many optional parts, staged assembly, or validation-heavy setup.

Solution

How the pattern responds

Use a builder to assemble the object step by step with clearer intent and validation points.

Structure

How the moving parts fit together

A builder accumulates construction state and produces a finished object through an explicit build step.

Java Example

Java example in the Order Management domain

Builder Pattern in Java example
package org.javaomnibus.ecommerce.gof.creational;

public final class OrderReceipt {
    private final String orderId;
    private final String customerEmail;
    private final boolean includeTaxBreakdown;
    private final boolean includeShipmentDetails;

    private OrderReceipt(Builder builder) {
        this.orderId = builder.orderId;
        this.customerEmail = builder.customerEmail;
        this.includeTaxBreakdown = builder.includeTaxBreakdown;
        this.includeShipmentDetails = builder.includeShipmentDetails;
    }

    public static final class Builder {
        private final String orderId;
        private final String customerEmail;
        private boolean includeTaxBreakdown;
        private boolean includeShipmentDetails;

        public Builder(String orderId, String customerEmail) {
          this.orderId = orderId;
          this.customerEmail = customerEmail;
        }

        public Builder includeTaxBreakdown() {
          this.includeTaxBreakdown = true;
          return this;
        }

        public Builder includeShipmentDetails() {
          this.includeShipmentDetails = true;
          return this;
        }

        public OrderReceipt build() {
          return new OrderReceipt(this);
        }
    }
}
Code Walkthrough

Step by step

  1. The builder makes optional configuration readable and explicit.
  2. The final build step creates one coherent object after assembly is complete.
  3. This is often much clearer than telescoping constructors.
  4. The pattern is about construction clarity more than about variation in product families.
When To Use

Where this pattern helps

  • When objects have many optional fields or staged assembly rules
  • When readability of construction matters as much as the final object
When Not To Use

When a simpler design is better

  • When object creation is simple enough for constructors or static factories
  • When the builder adds more ceremony than clarity
Trade-Offs

What this pattern costs

  • Adds a helper type and more lines of code
  • Can be overused for very simple objects
Modern Relevance

How this fits today

Builder remains highly relevant in Java, especially where immutability, optional configuration, and readable assembly matter.

Common Misuse

Where teams get it wrong

Adding builders to trivial value objects with two or three obvious fields often creates ceremony without improving readability.

Related Patterns

Compare with nearby choices


What To Read Next