1. Builder model concept
Definition: Separate a complex object construction from its representation so that the same construction process can create different representations;
Core: Separate construction and representation, different representations are different from abstract factory models when constructing:
(1) Similar to the abstract factory pattern, because it can also create complex objects. The main difference is that the builder model focuses on constructing a complex object step by step, focusing on the order of part type and assembly process. The abstract factory model focuses on multiple series of product objects (simple or complex). The builder pattern returns the product in the last step, and for the abstract factory, the product returns immediately.
(2) In the builder mode, there is a supervisor who manages the builder. The user contacts the supervisor, and the supervisor contacts the builder and finally gets the product. That is, the construction model can force a step-by-step construction process.
2. Its role
(1) Abstract Builder role: This role is used to regulate various components of the product and abstract it, which is generally independent of the logic of the application.
(2) Concrete Builder role:
This role implements all methods defined in the abstract builder and returns a built-in product instance.
(3) Product (Product) role:
This role is a complex object in construction, and there will be more than one product category in a system. These products do not necessarily have public interfaces and can be completely unrelated.
(4) Director role:
The character is responsible for arranging the order of existing modules and then telling the builder Builder to start building.
3. Example
public interface Builder { void buildPartA(); void buildPartB(); void buildPartC(); } public class BuilderImpl implements Builder { @Override public void buildPartA() { System.out.println("Build Part A"); } @Override public void buildPartB() { System.out.println("Build Part B"); } @Override public void buildPartC() { System.out.println("Build Part C"); } } public class BuilderImpl2 implements Builder { @Override public void buildPartA() { System.out.println("Build Part AA"); } @Override public void buildPartB() { System.out.println("Build Part BB"); } @Override public void buildPartC() { System.out.println("Build Part CC"); } } /** * Commander: guide how to build * Combination Builder */ public class Director { private Builder builder; public Director(Builder builder) { this.builder = builder; } /** * Construction method: Defines the construction process* If other processes are needed to implement it, you can new one commander Director */ public void construct() { System.out.println("director commands builder for construction"); builder.buildPartA(); builder.buildPartB(); builder.buildPartC(); } } public class Director2 { private Builder builder; public Director2(Builder builder) { this.builder = builder; } /** * Construction method: Defines the construction process* If other processes are needed to implement it, you can new one commander Director */ public void construct() { System.out.println("director2 commands builder for construction"); builder.buildPartB(); builder.buildPartC(); builder.buildPartA(); } } public class Test { public static void main(String[] args) { Builder builder = new BuilderImpl(); Director director = new Director(builder); /* * The same construction process, different modules (buildPartA, buildPartB, buildPartC) implementation, new one builder implementation* Different construction processes, new one director * Different construction processes, different module implementation, new director, new builder */ director.construct(); System.out.println(""); Builder builder2 = new BuilderImpl2(); Director director2 = new Director(builder2); director2.construct(); System.out.println(""); Builder build3 = new BuilderImpl2(); Director2 director3 = new Director2(builder3); director3.construct(); } }Print:
director command builder for construction construction construction parts A construction part B construction part C director command builder for construction construction parts AA construction part BB construction part CC director2 command builder for construction parts BB construction part CC construction part AA
4. Advantages and Disadvantages (1) Advantages:
A. Can allow you to change the internal representation of the product.
B. Encapsulate the building and representative code.
C. Provide control over steps beyond the construction process.
(2) Disadvantages:
A. There is a need to create a variety of different types of products individually ConcreteBuilder.
5. Usage scenarios:
(1) When the algorithm for creating complex objects should be independent of the components of the object and how they are assembled.
(2) When the construction process must allow the constructed object to have different representations (same method, different execution order, and different results are produced).