Fundamentals 5 min read

Builder Design Pattern: Introduction, UML Diagram, Java Implementation, Use Cases, Advantages and Disadvantages

This article introduces the Builder design pattern, explains its role in separating object construction from representation, provides UML class diagrams and Java code examples, outlines typical application scenarios, and discusses its advantages and disadvantages.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Builder Design Pattern: Introduction, UML Diagram, Java Implementation, Use Cases, Advantages and Disadvantages

Introduction – The Builder pattern is a creational design pattern that separates the construction of a complex object from its representation, allowing the same construction process to create different representations.

UML Class Diagram – The pattern involves four main participants: Builder (abstract class or interface defining creation steps), ConcreteBuilder (implements the steps), Director (orchestrates the building process), and Product (the complex object).

Java Code Example

class Product {
    private String partA;
    private String partB;
    private String partC;
    public void setPartA(String partA) { this.partA = partA; }
    public void setPartB(String partB) { this.partB = partB; }
    public void setPartC(String partC) { this.partC = partC; }
    public void show() { /* display product features */ }
}

abstract class Builder {
    protected Product product = new Product();
    public abstract void buildPartA();
    public abstract void buildPartB();
    public abstract void buildPartC();
    public Product getResult() { return product; }
}

public class ConcreteBuilder extends Builder {
    public void buildPartA() { product.setPartA("Builder PartA"); }
    public void buildPartB() { product.setPartB("Builder PartB"); }
    public void buildPartC() { product.setPartC("Builder PartC"); }
}

class Director {
    private Builder builder;
    public Director(Builder builder) { this.builder = builder; }
    public Product construct() {
        builder.buildPartA();
        builder.buildPartB();
        builder.buildPartC();
        return builder.getResult();
    }
}

public class Client {
    public static void main(String[] args) {
        Builder builder = new ConcreteBuilder();
        Director director = new Director(builder);
        Product product = director.construct();
        product.show();
    }
}

Application Scenarios – The Builder pattern is suitable when a class has many constructor parameters leading to telescoping constructors, when different representations of an object are needed, or when the construction process is complex and should be isolated from business logic.

Advantages

Adheres to the Single Responsibility Principle by extracting construction code from business logic.

Improves encapsulation; object creation and representation are decoupled.

Open/Closed Principle: new builders can be added without modifying existing code.

Enhances extensibility; concrete builders are independent, facilitating system decoupling.

Disadvantages

Increases code complexity and maintenance effort.

High similarity among concrete builders limits applicability.

Changes in the product’s internal structure require corresponding updates in builders, raising maintenance cost.

Mind Map

In summary, mastering the Builder pattern helps developers create complex objects in a clean, maintainable way, especially when dealing with numerous optional parameters or intricate construction processes.

design patternsJavasoftware architectureUMLObject CreationBuilder Pattern
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.