Friday, 29 November 2013

Builder Design pattern - Class Diagram

Master Software Architecture & Clean Code!

Subscribe to Ram N Java for clean code, design pattern implementations, and practical Java tutorials!

SUBSCRIBE NOW

Understanding the Builder Pattern Class Diagram

The Builder Design Pattern is a GoF creational pattern that decouples the construction of a complex object from its internal representation. While understanding the conceptual theory is helpful, mastering the UML Class Diagram is essential to designing modular, extensible object architectures.

A standard Builder class diagram defines four core participants and maps out their structural dependencies, inheritance hierarchies, and aggregation relationships.

Key Participants in the Class Diagram

  • Product: The complex object being assembled, containing multiple fields and nested components.
  • Builder (Interface / Abstract Class): Defines the contract of discrete assembly operations (e.g., buildPartA(), buildPartB()) and the product retrieval method.
  • ConcreteBuilder: Implements the Builder interface, tracks intermediate states, and instantiates the concrete Product.
  • Director: Holds an aggregation reference to a Builder instance and coordinates the step-by-step assembly process via a construction method (e.g., construct()).
  • Client: Configures the Director with a specific ConcreteBuilder and retrieves the final product.

Clean Java Code Mapping to the Diagram

Here is an end-to-end Java implementation demonstrating how these UML relationships translate directly into production-ready code:

// 1. Product
class Computer {
    private String cpu;
    private String ram;
    private String storage;

    public void setCpu(String cpu) { this.cpu = cpu; }
    public void setRam(String ram) { this.ram = ram; }
    public void setStorage(String storage) { this.storage = storage; }

    @Override
    public String toString() {
        return "Computer [CPU=" + cpu + ", RAM=" + ram + ", Storage=" + storage + "]";
    }
}

// 2. Builder Interface
interface ComputerBuilder {
    void buildCpu();
    void buildRam();
    void buildStorage();
    Computer getComputer();
}

// 3. ConcreteBuilder
class GamingComputerBuilder implements ComputerBuilder {
    private Computer computer = new Computer();

    @Override
    public void buildCpu() { computer.setCpu("High-Performance Octa-Core CPU"); }

    @Override
    public void buildRam() { computer.setRam("32GB DDR5 RAM"); }

    @Override
    public void buildStorage() { computer.setStorage("2TB NVMe SSD"); }

    @Override
    public Computer getComputer() { return this.computer; }
}

// 4. Director (Aggregates ComputerBuilder)
class ComputerAssembler {
    private ComputerBuilder builder;

    public ComputerAssembler(ComputerBuilder builder) {
        this.builder = builder;
    }

    public void construct() {
        builder.buildCpu();
        builder.buildRam();
        builder.buildStorage();
    }

    public Computer getComputer() {
        return builder.getComputer();
    }
}

// 5. Client
public class ClassDiagramDemo {
    public static void main(String[] args) {
        ComputerBuilder builder = new GamingComputerBuilder();
        ComputerAssembler director = new ComputerAssembler(builder);

        director.construct();
        Computer gamingPc = director.getComputer();

        System.out.println("Configured: " + gamingPc);
    }
}

Watch More Related Design Pattern Tutorials


Click here to watch in Youtube : https://www.youtube.com/watch?v=EHS4h2noiMQ

Click the below Image to Enlarge
Builder Design pattern - Class Diagram

















See also:

  • Builder Design Pattern - Introduction
  • Builder Design pattern - Real Time Example [Meal Package]
  • Builder Design pattern - Real Time Example [Animal Toys]
  • Builder Design pattern - Real Time Example [Beverage]
  • Builder Design Pattern - Sequence Diagram
  • Builder Design Pattern - Implementation [Beverage]
  • Builder Design Pattern - Implementation [Animal Toy]
  • Builder Design Pattern - KeyPoints
  • All Design Patterns Links
  • No comments:

    Post a Comment

    Tutorials