define

Builder mode is the object creation mode, which can separate the internal representation of a product from the process of product creation, so that a construction process can generate product objects with different internal representation

The builder mode hides the structure of the product and the construction process of the product’s parts from the client, separates the responsibility of directing the construction process from the responsibility of the specific builder’s parts, and achieves the purpose of responsibility division and encapsulation

intentions

Separating a complex build from its representation allows the same build process to create different representations

Main problem solving

In the software system, sometimes we are faced with the creation of “a complex object”, which is usually composed of sub-objects of each part with a certain algorithm. The pieces of this complex object often face drastic changes due to changing requirements, but the algorithms that put them together are relatively stable

When to use

When the basic components do not change, but the combination often changes

The advantages and disadvantages

Advantages:

  • The builder is independent and easy to expand
  • Easy to control risk details

Disadvantages:

  • Products must have something in common and be limited in scope
  • If the internal changes are complex, there are many builder classes

structure

Roles involved:

  • The role of Abstract Builder: An abstract interface is given to regulate the construction of the components of a product object. In general, the number of parts in a product is equal to the number of construction methods; in other words, there are as many parts as there are corresponding construction methods
  • The ConcreteBuilder role: Implements the interface declared by the abstract builder, giving step-by-step actions to create product instances; Provide examples of products after the construction process is complete
  • Director role: The class that holds this role calls the concrete builder role to create product objects. The Director role does not have specific knowledge of the product class, but the concrete builder role does
  • Product role: A Product is a complex object under construction. Generally speaking, there may be more than one Product class in a system, and these Product classes do not necessarily have a common interface, but can be completely unrelated

In general, for every product class, there is a corresponding concrete builder class, the products should have the same number of parts, and for every part, there is a corresponding build method among all the builder roles

The source code is as follows:

public interface Builder {

    /** Product parts construction method */
    void buildPart1(a);

    /** Product parts construction method */
    void buildPart2(a);

    /** Product return method */
    Product retrieveResult(a);
}
Copy the code
public class ConcreteBuilder implements Builder {

    private Product product = new Product();

    /** Product parts construction method */
    @Override
    public void buildPart1(a) {
        // Build the first part of the product
    }

    /** Product parts construction method */
    @Override
    public void buildPart2(a) {
        // Build the second part of the product
    }

    /** Product return method */
    @Override
    public Product retrieveResult(a) {
        returnproduct; }}Copy the code
public class Director {
    private Builder builder;

    /** * product constructor, responsible for calling individual part constructor */
    public void construct(a) {
        builder = newConcreteBuilder(); builder.buildPart1(); builder.buildPart2(); builder.retrieveResult(); }}Copy the code
public class Product {}Copy the code

The example of building a computer

Computer assembly has memory, motherboard, CPU, chassis, etc., can be assembled in accordance with different steps

Computer class:

public class Compute {

    /** cpu */
    private String cpu;

    / * * * / the main board
    private String board;

    / * * * / memory
    private String memory;

    Chassis / * * * /
    private String crate;

    public String getCpu(a) {
        return cpu;
    }

    public void setCpu(String cpu) {
        this.cpu = cpu;
    }

    public String getBoard(a) {
        return board;
    }

    public void setBoard(String board) {
        this.board = board;
    }

    public String getMemory(a) {
        return memory;
    }

    public void setMemory(String memory) {
        this.memory = memory;
    }

    public String getCrate(a) {
        return crate;
    }

    public void setCrate(String crate) {
        this.crate = crate;
    }

    @Override
    public String toString(a) {
        return "Compute{" +
                "cpu='" + cpu + '\' ' +
                ", board='" + board + '\' ' +
                ", memory='" + memory + '\' ' +
                ", crate='" + crate + '\' ' +
                '} '; }}Copy the code

Abstract the builder role, abstracting the steps of assembling a computer:

public interface ComputeBuilder {

    /** Assemble the CPU */
    void buildCpu(a);

    /** Assemble the motherboard */
    void buildBoard(a);

    /** Assemble memory */
    void buildMemory(a);

    /** Assemble the chassis */
    void buildCrate(a);

    /** Complete, return a computer */
    Compute getCompute(a);
}
Copy the code

Specific role of builder, the operation of assembling individual parts:

public class ConcreteComputeBuilder implements ComputeBuilder {

    private Compute compute = new Compute();

    @Override
    public void buildCpu(a) {
        System.out.println("Assemble the CPU");
        compute.setCpu("Assemble the CPU");
    }

    @Override
    public void buildBoard(a) {
        System.out.println("Assemble the motherboard.");
        compute.setBoard("Assemble the motherboard.");
    }

    @Override
    public void buildMemory(a) {
        System.out.println("Install memory");
        compute.setMemory("Install memory");
    }

    @Override
    public void buildCrate(a) {
        System.out.println("Install the chassis");
        compute.setCrate("Install the chassis");
    }

    @Override
    public Compute getCompute(a) {
        returncompute; }}Copy the code

Start assembly:

public class ComputeDirector {

    private ComputeBuilder builder;

    public void assemblyCompute(a) {
        builder = newConcreteComputeBuilder(); builder.buildCpu(); builder.buildBoard(); builder.buildMemory(); builder.buildCrate(); System.out.println(builder.getCompute()); }}Copy the code

The test class:

public class Client {
    public static void main(String[] args) {
        ComputeDirector director = newComputeDirector(); director.assemblyCompute(); }}Copy the code