Builder Mode of Design Mode 5

A complex object is often composed of multiple sub-components in certain steps. For example, a car is composed of components such as engines, tires, and steering wheels.

The various parts that make up the object can be flexibly selected, and the creation steps are similar. We can use the builder model to describe the creation of such products.

What is the builder mode

Separate the construction of a complex object from its representation so that the same construction process can create different representations.

In the builder mode, a complex object is decomposed into multiple simple objects, and the components of the product remain the same, but each simple object can be flexible and changeable.

Let's not talk about the advantages and disadvantages of the builder mode, now let's understand the structure of the builder mode.

The builder mode is mainly composed of four elements: product, abstract builder, concrete builder, and commander. The description of these 4 elements is as follows:

  • Product role (Product): It is a complex object containing multiple components, and each component is created by a specific builder.

  • Abstract builder (Builder): It is an interface that contains abstract methods for creating various subcomponents of a product, and usually contains a method for returning complex products  getResult().

  • Concrete Builder (Concrete Builder): implements the Builder interface and completes the concrete creation method of each component of a complex product.

  • 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 specific product information.

The relationship between these elements is as follows:

Builder mode

Code

Product categoryProduct

@Data
public class Product {
    private String partA;
    private String partB;
    private String partC;

    public void show(){
        String partA = getPartA();
        String partB = getPartB();
        String partC = getPartC();
        System.out.println("这可能是东半球最好的产品...");
        Console.log("组成结构:{},{},{}",partA,partB,partC);
    }
}

ProductThe class includes 3 component fields and a display method show().

Builder abstract classBuilder

public abstract class Builder {
 protected Product product = new Product();

 public abstract void buildPartA();

 public abstract void buildPartB();

 public abstract void buildPartC();

 public Product getResult() {
  return product;
 }
}

The abstract class Builderincludes 3 abstract methods and a Productmethod to obtain an object.

CommanderDirector

public class Director {
    private Builder builder;

    public Director(Builder builder) {
        this.builder = builder;
    }

    public Product construct(){
        builder.buildPartA();
        builder.buildPartB();
        builder.buildPartC();
        return builder.getResult();
    }
}

DirectorThe class contains a Directorconstructor to pass in the Builderobject, and the construct()method mainly calls the method in the built class and returns the Productobject.

ConcreteBuilderInherit the abstract builder and implement the methods inside.

public class ConcreteBuilder extends Builder {
    @Override
    public void buildPartA() {
        product.setPartA("建造 PartA");
    }

    @Override
    public void buildPartB() {
        product.setPartB("建造 PartB");
    }

    @Override
    public void buildPartC() {
        product.setPartC("建造 PartC");
    }
}

Call classClient

public class Client {
    public static void main(String[] args){
        Builder builder = new ConcreteBuilder();
        Director director = new Director(builder);
        Product construct = director.construct();
        construct.show();
    }
}

Test Results:

这可能是东半球最好的产品...
组成结构:建造 PartA,建造 PartB,建造 PartC

There is code to know:

First, a builder is created. There are various functions in the builder to assign values ​​to product attributes

Then a commander is created, and the commander calls the builder function to get the product

From the construction mode, we know that the product is not created directly, but is created by the conductor commanding the builder. Is this very similar to the factory mode, but it should be noted that the builder mode is more complicated than the factory mode.

When the product that needs to be created has a complex creation process, the common creation process can be extracted, and then handed over to the specific implementation class to customize the creation process, so that the same creation behavior can produce different products, separate creation and presentation, and create products The flexibility is greatly increased.

Thinking about the builder model

So what are the advantages of the builder mode?

Like the factory model, the client can obtain the product without knowing the internal composition details of the product; the builder and the builder are independent of each other. Conducive to system expansion; and modifying a certain builder will not affect other modules.

So when to use the builder mode? The Zen of Design Patterns wrote:

● The builder mode can be used when the same method, different execution sequence, and different event results are generated.

● Multiple components or parts can be assembled into one object, but the running results produced are not the same, you can use this mode.

● The product category is very complicated, or the calling sequence in the product category produces different performance. At this time, it is very suitable to use the builder mode.

● In the process of object creation, some other objects in the system are used. When these objects are not easily available during the creation of product objects, the builder mode can also be used to encapsulate the creation process of the object. This kind of scene can only be a compensation method, because an object is not easy to obtain, and it is not noticed in the design stage, and the creation process is softened through the creator mode, which itself violates the design.

Recommended in the past

Scan the QR code to get more exciting. Or search Lvshen_9 on WeChat , you can reply to get information in the background

1.回复"java" 获取java电子书;

2.回复"python"获取python电子书;

3.回复"算法"获取算法电子书;

4.回复"大数据"获取大数据电子书;

5.回复"spring"获取SpringBoot的学习视频。

6.回复"面试"获取一线大厂面试资料

7.回复"进阶之路"获取Java进阶之路的思维导图

8.回复"手册"获取阿里巴巴Java开发手册(嵩山终极版)

9.回复"总结"获取Java后端面试经验总结PDF版

10.回复"Redis"获取Redis命令手册,和Redis专项面试习题(PDF)

11.回复"并发导图"获取Java并发编程思维导图(xmind终极版)

Another: Click [ My Benefits ] to have more surprises.

Guess you like

Origin blog.csdn.net/wujialv/article/details/109008043