There are three main types of design patterns that are treated differently in design patterns: creation patterns, structural patterns, and behavior patterns.

Abstract Factory Pattern Builder Pattern Prototype Pattern Singleton Pattern

define

The creator pattern, also known as the builder pattern, separates the construction of a complex object from its representation, allowing the same construction process to create different representations. Creator mode hides the creation process of complex objects. It abstracts the creation process of complex objects and dynamically creates objects with compound properties through subclass inheritance or overloading.

I understand it

The builder pattern THAT I understand is to create a complex object that has a lot of properties. Sometimes you don’t need a lot of attributes to be preconfigured and you just need a few important attributes to be initialized! The builder pattern is initialized for each important property by chaining to produce a complete object

Creator mode code

/ * * *@author: yangqiang
 * @create: every 2021-02-22 * /
public class Course {
    private String name;
    private String means;
    private String note;
    private String homework;

    public String getName(a) {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMeans(a) {
        return means;
    }

    public void setMeans(String means) {
        this.means = means;
    }

    public String getNote(a) {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public String getHomework(a) {
        return homework;
    }

    public void setHomework(String homework) {
        this.homework = homework;
    }

    public static class CourseBuilder {
        private Course course = new Course();

        public CourseBuilder addName(String name) {
            course.setName(name);
            return this;
        }

        public CourseBuilder addMeans(String means) {
            course.setMeans(means);
            return this;
        }

        public CourseBuilder addNote(String note) {
            course.setNote(note);
            return this;
        }

        public CourseBuilder addHomework(String homework) {
            course.setHomework(homework);
            return this;
        }

        public Course build(a) {
            returncourse; }}}Copy the code

advantages

  • Using the builder pattern eliminates the need for the client to know the details of the product’s internal composition.
  • The concrete builder classes are independent of each other, which facilitates the expansion of the system.
  • The specific builders are independent of each other, so the building process can be gradually refined without any impact on the other modules.

disadvantages

  • The builder pattern creates products that generally have more in common and have similar components; If the difference between products is large, the builder pattern is not suitable, so its use is limited.
  • If the internal changes in the product are complex, many concrete builder classes may need to be defined to implement the changes, resulting in a large system.

In general, we prefer to implement the Builder pattern using static inner classes, where a product class automatically has a specific Builder inside it that is responsible for the assembly of the product, no Builder or Director required. Doing so creates a tighter connection between product presentation and creation, a more compact structure, and a cleaner builder pattern. If the builder pattern is implemented as a static inner class, the example in the section “Builder Pattern implements chained assignments” can be rewritten as follows.

Contrast this with the abstract factory pattern

  • In contrast to the abstract factory pattern, where the builder pattern returns an assembled complete product, the abstract factory pattern returns a set of related products that are located in different product-level structures and constitute a family of products.
  • In the abstract factory pattern, the client class instance chemical plant, and then call the factory method to obtain the required product object, in the builder pattern, the client can not directly call the builders of relevant methods, but by commanders class to guide how to generate object, including the object of the assembly process and construction steps, step by step, it focuses on the construction of a complex object, Returns a complete object.
  • If you think of the abstract factory pattern as an auto parts factory that produces the products of a product family, then the builder pattern is an auto assembly factory that returns a complete car by assembling the parts