Creational mode - Builder mode (Builder) - solve the problem of complex object creation

Creational patterns

Builder mode (Builder)

Solve complex object creation problems

describe

By decomposing the construction process of a complex object into multiple simple object construction steps and defining a unified construction interface, the client can obtain a complete complex object without knowing the specific construction details and order. This method can improve the readability and maintainability of the code, and can also support different construction processes and construction schemes.

Applicable environment

The objects that need to be created are complex and consist of multiple parts, and the construction process is relatively stable. Complete objects with different properties need to be created in different situations.

advantage:

Separating the construction process of a complex object into multiple simple object construction steps makes the system more flexible. Complete objects with different properties can be created through different construction sequences and methods.

shortcoming:

The builder mode needs to add many new classes, which increases the complexity of the system.

violation of principles

Possible violation of the open-closed principle: if a component is added or removed, the concrete builder class needs to be modified.
Possible Liskov Substitution Principle Violation: The type of product returned by the builder may vary.

Code

background description

Books generally contain four properties: title, author, publication date, and ISBN number. If
you want to create a new Book object, but don't pass all the properties to it through the constructor, this will lead to lengthy and difficult to maintain code.
Use the builder pattern to Separate the construction of the book object from its representation, and make the construction process easier and easier to use.
The following is the Java code implementation of the above example:

//书籍类
class Book {
    
    
    private String title;
    private String author;
    private String publicationDate;
    private String isbn;

    // 私有化构造方法
    private Book(String title, String author, String publicationDate, String isbn) {
    
    
        this.title = title;
        this.author = author;
        this.publicationDate = publicationDate;
        this.isbn = isbn;
    }

    public String getTitle() {
    
    
        return title;
    }

    public String getAuthor() {
    
    
        return author;
    }

    public String getPublicationDate() {
    
    
        return publicationDate;
    }

    public String getIsbn() {
    
    
        return isbn;
    }
  
    // 静态内部类Builder
    public static class Builder {
    
    
        private String title;
        private String author;
        private String publicationDate;
        private String isbn;

        // 设置书名
        public Builder setTitle(String title) {
    
    
            this.title = title;
            return this;
        }

        // 设置作者
        public Builder setAuthor(String author) {
    
    
            this.author = author;
            return this;
        }

        // 设置出版日期
        public Builder setPublicationDate(String publicationDate) {
    
    
            this.publicationDate = publicationDate;
            return this;
        }

        // 设置ISBN号码
        public Builder setIsbn(String isbn) {
    
    
            this.isbn = isbn;
            return this;
        }

        // 构建Book对象
        public Book build() {
    
    
            return new Book(title, author, publicationDate, isbn);
        }
    }
}

In the code example, the constructor of the Book class is privatized, and a static internal class Builder is added.
The Builder class contains four setter methods to set the properties of the Book object, and calls the private constructor of the Book class to build a complete Book object.

Using the builder pattern, we can create a new Book object in the following way.
Doing so makes the construction process more intuitive and concise, and avoids a large number of constructor overloads, thereby improving code readability and maintainability



Book book = new Book.Builder()
                    .setTitle("战争与和平")
                    .setAuthor("列夫·托尔斯泰")
                    .setPublicationDate("1869年至1870年")
                    .setIsbn("978-7-111-54803-2")
                    .build();
                    

Guess you like

Origin blog.csdn.net/u010349629/article/details/130029112