Detailed explanation and example of Java builder pattern builder pattern
Java Builder Pattern
1. Concept
Separating a complex build from its representation allows the same build process to create different representations. [separation of construction and representation, isomorphism and different representation]
Difference from abstract factory: in the builder mode, there is a guide, and the guide manages the builder. The user contacts the guide, and the guide contacts the builder to get the product. That is, the construction mode can enforce a step-by-step construction process.
The construction mode encapsulates the complex internal creation inside. For external callers, they only need to pass in the builder and construction tools. The callers do not need to care about how the internal is built into a finished product.
For example, a car has many parts, such as wheels, steering wheel, engine and various small parts. There are many parts, but they are far more than these. How to assemble these parts into a car is also very complex (requires good assembly technology). The builder mode is to separate the parts from the assembly.
2. UML diagram
3. Code
The following is the method to call Builder:
4. Application scenarios
JavaMail uses this pattern in Java applications.
Thank you for reading, hope to help you, thank you for your support to this site!