Builder pattern is to use simple object step by step, the structure of the complex object, for example, you want to construct a robot, the constructor is usually create a robot object to you directly, and using the builder pattern is step by step, to create your head first, then to create the body you, step by step, finally to create a complete robot objects.

Let’s implement the Builder pattern in code.

First of all, we need to build the robot, so we have to have the entity class of the robot and the entity class of the parts:

public class Robot {
    private Head head;
    private Body body;
    private Hand hand;
    private Leg leg;
    private Foot foot;
    // omit the set method
}
public class Body {}public class Hand {}public class Head {}public class Leg {}public class Foot {}Copy the code

All construction robots can be divided into head construction, body construction, hand construction, leg construction and foot construction, which can be abstracted as the interface construction of the robot:

public interface RobotBuilder {
    void buildHead(a);
    void buildBody(a);
    void buildHand(a);
    void buildLeg(a);
    void buildFoot(a);
    Robot build(a);
}
Copy the code

XM wanted to build robots and they implemented the robot construction interface, which implemented the construction of all the components:

public class XMRobotBuilder implements RobotBuilder{

    private Robot robot = new Robot();

    @Override
    public void buildHead(a) {
        robot.setHead(new Head());
    }

    @Override
    public void buildBody(a) {
        robot.setBody(new Body());
    }

    @Override
    public void buildHand(a) {
        robot.setHand(new Hand());
    }

    @Override
    public void buildLeg(a) {
        robot.setLeg(new Leg());
    }

    @Override
    public void buildFoot(a) {
        robot.setFoot(new Foot());
    }

    @Override
    public Robot build(a){
        returnrobot; }}Copy the code

Now we can manufacture robots, but we lack something to guide the whole manufacturing process, so the Director appears:

public class Director {

    private RobotBuilder robotBuilder;

    public Director(RobotBuilder robotBuilder) {
        this.robotBuilder = robotBuilder;
    }

    public Robot construct(a){
        robotBuilder.buildHand();
        robotBuilder.buildBody();
        robotBuilder.buildHead();
        robotBuilder.buildLeg();
        robotBuilder.buildFoot();
        returnrobotBuilder.build(); }}Copy the code

Now that the assembly line and the manufacturers of individual components are all ready, we can start manufacturing robots. We first activated the Director, the manager of the process, and told him to make robots of XM Company. Then we gave the order to start construction, and a robot was made.

public class Main {
    public static void main(String[] args) {
        Director director = new Director(newXMRobotBuilder()); Robot robot = director.construct(); System.out.println(robot.toString()); }}Copy the code

The results are as follows, and you can see that all the components of the robot have been manufactured successfully.

In the above example, the robot’s head, body, hands, legs, and feet can all be declared as interfaces, and the corresponding XM company can implement the details of building these components itself. This is not done for simplicity, but for understanding the builder pattern.

Builder mode benefits:

  1. Consumers do not need to know how complex objects are built.
  2. The specific builders are independent of each other, facilitating the system’s expansion.
  3. The Director role of the builder can be assumed by the consumer, who can decide which process to follow and which properties of the object to create, for example in Lombok@BuilderAnnotation, when applied to an entity class, you can use it to initialize any number of member variables of your object by chain assignment, without being limited to the class not having the constructor you want.

Disadvantages of builder mode:

  1. The products you build must have something in common.
  2. Each product must have a corresponding build class, adding to the complexity of the system.