Small talk about design patterns (14)—Builder pattern

Column introduction

Column address

link

Column introduction

It mainly analyzes and summarizes the 23 common design patterns currently on the market one by one. I hope that interested friends can take a look and it will be continuously updated. I hope you can supervise me and we can learn and make progress together. Come on, everyone.

builder pattern

The builder pattern is a creational design pattern that separates the construction process of a complex object from its representation, so that the same construction process can create different representations.
Insert image description here

Role classification

Product

Represents a complex object being constructed. Usually contains multiple parts, such as properties, methods, etc.

Abstract Builder

Defines abstract methods for building a product, usually including methods for building individual parts and methods for returning the product.

Concrete Builder

Implemented the abstract builder interface and is responsible for the specific product building process. Typically contains an instance of a specific product that is ultimately returned by building the various parts.

Director

It is responsible for calling specific builders to build products. It does not know the specific construction details and is only responsible for calling the build method and returning products.

Insert image description here

main idea

Break down the process of building complex objects into multiple simple steps, implement these steps through different concrete builders, and finally the commander calls the methods of the concrete builders to build the product. This makes the building process more flexible, allowing different specific builders to be selected to build different products as needed.
Insert image description here

Java program

// 产品类
class Product {
    
    
    private String part1;
    private String part2;
    
    public void setPart1(String part1) {
    
    
        this.part1 = part1;
    }
    
    public void setPart2(String part2) {
    
    
        this.part2 = part2;
    }
    
    public void show() {
    
    
        System.out.println("Part 1: " + part1);
        System.out.println("Part 2: " + part2);
    }
}

// 抽象建造者
interface Builder {
    
    
    void buildPart1();
    void buildPart2();
    Product getResult();
}

// 具体建造者
class ConcreteBuilder implements Builder {
    
    
    private Product product;
    
    public ConcreteBuilder() {
    
    
        product = new Product();
    }
    
    public void buildPart1() {
    
    
        product.setPart1("Part 1");
    }
    
    public void buildPart2() {
    
    
        product.setPart2("Part 2");
    }
    
    public Product getResult() {
    
    
        return product;
    }
}

// 指挥者
class Director {
    
    
    private Builder builder;
    
    public Director(Builder builder) {
    
    
        this.builder = builder;
    }
    
    public void construct() {
    
    
        builder.buildPart1();
        builder.buildPart2();
    }
}

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

program analysis

In the above code, we define a product class Product, which has two parts part1 and part2. Then we defined an abstract builder interface Builder, which contains methods for building various parts of the product and defines methods for obtaining the final product. Then we implemented the concrete builder ConcreteBuilder, which implements the builder interface and specifically implements the methods for building each part. Then we defined a director, which is responsible for controlling the construction process and building the product by calling the builder's method. Finally, in the client, we create a specific builder object and pass it into the commander, then use the commander to build the product, and finally obtain the built product and display it.

Insert image description here

Advantages and Disadvantages Analysis

advantage

1

You can separate the construction process of complex objects from their representations, allowing the same construction process to create different representations.

2

You can control the object's construction process more granularly and flexibly add, delete, or modify construction steps to create different products.

3

It can avoid too many parameters in the constructor and improve the readability and maintainability of the code.

4

The implementation details of specific products can be hidden through builders, and only the unified construction interface is exposed to improve the encapsulation of the code.
Insert image description here

shortcoming

1

Increases the complexity of the code and requires defining multiple classes and interfaces to implement the builder pattern.

2

If the product's components vary less, or if there is only one specific builder, the builder pattern may seem overly cumbersome.

Analyzed

The builder pattern is suitable for scenarios where complex objects are built. By decomposing the construction process into multiple steps, it makes the construction process more flexible and the same construction process can be reused to create different products. However, the builder pattern also increases the complexity of the code, and the advantages and disadvantages of using the builder pattern need to be weighed.

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_74888502/article/details/133524929