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!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>