1 Preface
Sometimes there are multiple ways to build some objects. If these logics are included in a single method of the class that builds these objects, the building logic will be ridiculous (for example, A large nested if-else or switch-case statement for various build requirements ). If the building process can be broken down into the relationship between client-ctor-builder, the process will be easier to manage and reuse. The design pattern for this type of relationship becomes a generator.
2. Details
2.1 Brief Introduction
In addition to the customer and the products required, the Builder mode contains the following roles: Director and Builder ). The Builder knows how to build a feature product without specific information. Director knows how the Builder should build and uses parameters to provide it with missing information to build a specific product.
The builder mode separates the construction of a complex object from its performance, so that the same building process can create different manifestations.
2.2 When to use
(1) complex objects involving various components must be created. The algorithm for creating objects should be independent of the assembly method of parts. A common example is to build a composite object.
(2) The build process requires building objects in different ways (for example, components or different combinations of performance.
2.3 comparison between generators and abstract factories
(1) generators construct complex objects and abstract factories to construct simple or complex objects;
(2) The generator constructs objects in multiple steps, and the abstract factory constructs objects in a single step;
(3) generators build objects in multiple ways, abstract factories build objects in a single step;
(4) The generator returns the product at the last step of the build process, and the abstract factory immediately returns the product;
(5) The generator focuses on a specific product, and the abstract factory emphasizes a set of products.
3 conclusion
The builder mode helps you build objects that involve components and representations.
The above is all content and I hope it will help you.