terminal

codeando_simple

terminal

menu

terminal

search_module

guest@codeandosimple: ~/system/search $ grep -r "" .

Press [ENTER] to execute search

Status

Engine: Ready

Database: Online

Index: V2.1.0_LATEST

bash -- cat builder.md
guest@codeandosimple: ~/blog/design-patterns $ cat builder.md

Builder Pattern_

// "You can always do it, when you want to" - José Luis Sampedro

# Purpose

The Builder pattern separates the construction of a complex object from its representation, so that the same construction process can create different representations.

# Problem

The need to create a complex object in a flexible and controlled way, avoiding having a constructor with a long list of parameters, which is not practical at all.

# Solution

The solution proposed by the Builder is:

  • Encapsulate object construction: Separate the construction from its representation. This allows the same construction process to create different representations of the object.

  • Step-by-step construction: The object is constructed step-by-step under the control of the "director", who ensures correct construction.

# Structure

Builder pattern structure

# Participants

  • Builder: Interface that declares the methods for creating the parts of a product object (Product).

  • ConcreteBuilder: Implements the Builder interface to construct and assemble the parts of the product. Defines the representation to be created and returns the product.

  • Director: Constructs an object using the Builder interface in a series of steps.

  • Product: Represents the complex object being constructed.

# When to use it

This pattern is recommended when the construction process:

  • Needs to be independent of the parts that make up the object.

  • Needs to allow different representations of the object being constructed.

# Advantages

  • verified

    Greater control

    Allows piecemeal construction, yielding finer control over the construction process.

  • verified

    Encapsulation and Clarity

    Encapsulates the code for construction and representation.

  • verified

    Reusability

    The director can be reused to create different variations of products.

# Disadvantages

  • warning

    Additional Complexity

    Introduces more classes and objects (director, builder, concrete builder).

  • warning

    Separate Builder

    A specific ConcreteBuilder must be created for each type of product.

# Example: Computer assembling system

We are building a system to configure custom computers. The computers can have different components (CPU, GPU, memory, etc.), and we want to let the customer choose these parts.

Problem

Customers must be able to create a computer from the configuration of each of its parts.

Proposed solution

We implement a Builder that allows creating a complex object (the computer) from the construction of each of its parts.

The ComputerBuilder interface defines the steps to assemble different computer components (CPU, GPU, memory). GamingComputerBuilder is a specific implementation that assembles a gaming computer following those steps. Computer is the final assembled computer, with the selected components. ComputerShop acts as the director, guiding the computer assembly using a ComputerBuilder.

Example Builder Computer

# Java Code

We code in Java what we prepared in the diagram. We define the Product:

class Computer {
    private String cpu;
    private String gpu;
    private String memory;

    public String getCpu() { return cpu; }
    public void setCpu(String cpu) { this.cpu = cpu; }
    public String getGpu() { return gpu; }
    public void setGpu(String gpu) { this.gpu = gpu; }
    public String getMemory() { return memory; }
    public void setMemory(String memory) { this.memory = memory; }
}

We define the builder interface:

interface ComputerBuilder {
    void buildCPU();
    void buildGPU();
    void buildMemory();
    Computer getComputer();
}

We implement a concrete builder for gaming computers:

class GamingComputerBuilder implements ComputerBuilder {
    private Computer computer = new Computer();

    public void buildCPU() { computer.setCpu("High-end CPU"); }
    public void buildGPU() { computer.setGpu("High-end GPU"); }
    public void buildMemory() { computer.setMemory("64GB RAM"); }
    public Computer getComputer() { return computer; }
}

We create the director, which uses the builder to construct the computer:

class ComputerShop {
    public Computer constructComputer(ComputerBuilder builder) {
        builder.buildCPU();
        builder.buildGPU();
        builder.buildMemory();
        return builder.getComputer();
    }
}

# Mapping Participants

  • ComputerBuilder(Builder): Interface that declares the methods for building parts of the product.

  • GamingComputerBuilder (ConcreteBuilder): Implements the Builder interface; saves and returns the final product.

  • Computer (Product): The complex object to build.

  • ComputerShop (Director): Defines the order in which the parts will be constructed.

# Conclusions

In this example we could see the use of the Builder pattern assembling computers with different specifications, in a structured and flexible way. Each component is selected and assembled step by step, facilitating customization for the client.

# Related Patterns

Factory Method

ConcreteBuilder classes are often implemented with factory methods.

Abstract Factory

Both build complex objects. Builder returns it as a final step; Abstract returns it immediately.

Composite

Often what the Builder constructs is a Composite.