Builder mode (Bulider mode)

Posted by endlyss on Tue, 18 Jan 2022 21:24:14 +0100

Definition and characteristics of pattern

Definition of Builder pattern: it refers to separating the construction of a complex object from its representation, so that the same construction process can create different representations Design pattern It is called the builder model. It decomposes a complex object into several simple objects, and then builds them step by step. It separates change from invariance, that is, the components of the product are invariable, but each part can be flexibly selected.

The main advantages of this mode are as follows:

  1. Good encapsulation, separation of construction and representation.
  2. Good expansibility, each specific builder is independent of each other, which is conducive to the decoupling of the system.
  3. The client does not need to know the details of the internal composition of the product. The builder can gradually refine the creation process without any impact on other modules, so as to control the detail risk.


Its disadvantages are as follows:

  1. The components of the product must be the same, which limits its scope of use.
  2. If the internal changes of the product are complex, if the internal changes of the product occur, the builder should also modify them synchronously, and the later maintenance cost is large.


The concerns of the Builder mode and the factory mode are different: the Builder mode focuses on the assembly process of parts, but Factory method model Pay more attention to the creation process of components, but the two can be used together.

Structure and implementation of pattern

The Builder pattern consists of four elements: product, abstract Builder, concrete Builder and commander. Now let's analyze its basic structure and implementation method.

1. Structure of the model

The main roles of the Builder pattern are as follows.

  1. Product role: it is a complex object containing multiple components, and its components are created by specific builders.
  2. Abstract Builder: it is an interface that contains abstract methods for creating various sub parts of a product, and usually contains a method getResult() that returns a complex product.
  3. Concrete Builder: implement the builder interface and complete the specific creation method of various components of complex products.
  4. Director: it calls the component construction and assembly methods in the builder object to complete the creation of complex objects. The director does not involve the information of specific products.


Its structure is shown in Figure 1.

2. Implementation of mode

Figure 1 shows the main structure of the Builder pattern, and the codes of its related classes are as follows.

(1) Product roles: complex objects that contain multiple components.

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() {
        //Displays the characteristics of the product
    }
}

(2) Abstract Builder: contains the abstract method of creating each sub part of the product.

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

(3) Concrete Builder: implements the abstract builder interface.

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

(4) Commander: call the method in the builder to complete the creation of complex objects.

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

(5) Customer class.

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 scenario of pattern

The only difference between the builder pattern and the factory pattern is the creation of complex objects. In other words, if you create simple objects, you usually use factory mode to create them. If you create complex objects, you can consider using builder mode.

When the product to be created has a complex creation process, you can extract the common creation process, and then hand it over to the specific implementation class to customize the creation process, so that the same creation behavior can produce different products, separate creation and representation, and greatly increase the flexibility of creating products.

The builder mode is mainly applicable to the following application scenarios:

  • The same method and different execution order produce different results.
  • Multiple assemblies or parts can be assembled into one object, but the results are different.
  • The product class is very complex, or different call sequences in the product class have different effects.
  • Initializing an object is particularly complex, with many parameters, and many parameters have default values.

Difference between builder mode and factory mode

Through the previous study, we have learned about the builder model, so what is the difference between it and the factory model?

  • The builder pattern pays more attention to the calling sequence of methods, and the factory pattern pays more attention to the creation of objects.
  • The intensity of creating objects is different. The builder mode creates complex objects, which are composed of various complex parts. The objects created by the factory mode are the same
  • The focus is different. The factory mode only needs to create the object, while the builder mode should not only create the object, but also know what components the object is composed of.
  • The builder mode is different according to the order of the construction process, and the composition of the final object components is also different.

Mode extension

The Builder mode can be changed as needed in the application process. If there is only one product type and only one specific Builder is required, the abstract Builder or even the conductor role can be omitted.