All supporting codes are on GitHub: Https://github.com/ZHONGHuanGit/DesignPattern
Brief introduction:
The builder pattern is also called the generator pattern, defined as: encapsulates the construction process of a product and constructs it by this step.
The builder pattern (builder pattern) belongs to the Create class pattern, and you'll feel a bit like it, but there's a difference, compared to the factory pattern. However, it is recommended that before you look at the three factory model, if you remember not very clear, you can see here, simple factory, factory method, abstract factory.
UML Class Diagram:
Take a look at the UML class diagram. This part, if not clear, can first look at the scene section below, and then look back.
From the class diagram above, you can see that there are four elements.
1 abstractbuilder (abstract builder): The purpose of introducing the abstract builder is to make the concrete process of construction into its subclasses. This makes it easier to expand. There are generally two parts of abstract methods, one for building products and the other for returning products. As above, BuildPart1 and BuildPart2 are used to construct products and Retrieveresult return products.
2 ConcreteBuilder (Concrete builders): Abstract methods for the realization of abstract builders, so as to facilitate expansion in different situations.
3 Director (director): responsible for calling the appropriate builders to build products, the Director class does not typically have a dependency on the product class, and direct interaction with the Director class is the Builder class. In general, the Director class is used to encapsulate variable parts of the program.
4 Products (Product Class): is generally a more complex object, that is, the process of creating objects is more complex, there will generally be more code. In this class diagram, the product class is a concrete class, not an abstract class. In practical programming, a product class can consist of an abstract class and its different implementations, or it can be composed of multiple abstract classes and their implementations.