Builder Pattern Explained: Simple Theory, Real‑World Scenarios & Java Code
This article introduces the Builder design pattern, illustrates its concept with everyday analogies like milk‑tea making, and provides a complete Java example that assembles a computer through step‑by‑step builders, followed by a summary of typical business use cases and a tip on avoiding confusion with the Factory pattern.
Builder Pattern Overview
The Builder pattern separates the construction of a complex object from its representation, allowing the same construction process to create different product variants. By extracting the step‑by‑step assembly logic, the pattern provides a unified workflow while enabling flexible combination of parts.
Real‑world analogies
Milk‑tea preparation: a fixed sequence (base → ingredients → sweetener) can produce milk tea, fruit tea, or smoothies by swapping ingredients.
Software scenario: a complex report where header, chart, text, and footer modules can be freely combined.
Other examples: API request parameter assembly, complex pop‑ups, house renovation with interchangeable walls, floors, and doors.
Java implementation (computer‑assembly example)
Product class
public class Computer {
private String cpu;
private String gpu;
private String memory;
// setters
public void setCpu(String cpu) { this.cpu = cpu; }
public void setGpu(String gpu) { this.gpu = gpu; }
public void setMemory(String memory) { this.memory = memory; }
@Override
public String toString() {
return "电脑配置:CPU="+cpu+", 显卡="+gpu+", 内存="+memory;
}
}Abstract builder
public abstract class ComputerBuilder {
protected Computer computer = new Computer();
public abstract void buildCpu();
public abstract void buildGpu();
public abstract void buildMemory();
public Computer getComputer() { return computer; }
}Concrete builders
// Game computer builder
public class GameComputerBuilder extends ComputerBuilder {
@Override
public void buildCpu() { computer.setCpu("i9"); }
@Override
public void buildGpu() { computer.setGpu("RTX4090"); }
@Override
public void buildMemory() { computer.setMemory("32G"); }
}
// Office computer builder
public class OfficeComputerBuilder extends ComputerBuilder {
@Override
public void buildCpu() { computer.setCpu("i3"); }
@Override
public void buildGpu() { computer.setGpu("核显"); }
@Override
public void buildMemory() { computer.setMemory("8G"); }
}Director (orchestrator)
public class Director {
public Computer createPC(ComputerBuilder builder) {
builder.buildCpu();
builder.buildGpu();
builder.buildMemory();
return builder.getComputer();
}
}Test client
public class Test {
public static void main(String[] args) {
Director director = new Director();
// Assemble a gaming computer
Computer gamePc = director.createPC(new GameComputerBuilder());
System.out.println(gamePc);
// Assemble an office computer
Computer officePc = director.createPC(new OfficeComputerBuilder());
System.out.println(officePc);
}
}Typical business scenarios
Construction steps are fixed but component combinations vary (complex forms, reports).
Entities with many parameters and cumbersome creation (request objects, order details).
Multiple versions of a similar complex object (exporting PDF/Excel/Word).
Complex UI pages, multi‑spec product assembly, diverse message payload construction.
Common pitfall
The Factory pattern focuses on directly producing a finished product, whereas the Builder pattern emphasizes assembling parts step by step; confusing the two can lead to inappropriate design choices.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
liandk
Seasoned Java and mobile developer with years of experience, specializing in mini‑programs, public accounts, and full‑stack front‑end development. In the AI era, I continuously learn to broaden my knowledge and evolve. I revived a public account I started a decade ago during a dessert‑startup venture, using code as a vessel and knowledge as a companion. I share personal projects, technical articles, programming tips, and growth insights—let’s improve together and set sail.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
