Separating a complex build from its presentation allows the same build process to create different representations. [Construction and presentation separation, different representations of construction]
The difference from the abstract factory: in the builder model, there is a mentor who manages the builder, the user is in contact with the mentor, and the mentor contacts the builder to get the product. That is, the construction model can enforce a process of construction in stages.
The building pattern is to encapsulate complex internal creation inside, and for external calls, only incoming builders and build tools are needed, and the caller does not need to be concerned about how the interior is built to create the finished product.
The factory class pattern provides a pattern for creating a single class, while the builder pattern is a collection of products that are managed to create a composite object that refers to a class that has different properties
Cases:
Public InterfaceBuilder {voidBuildparta (); voidBUILDPARTB (); voidBUILDPARTC (); Product GetResult (); } //Concrete Construction Tools Public classConcreteBuilderImplementsBuilder {Part parta, PartB, PARTC; Public voidBuildparta () {//here's how to build Parta code specifically}; Public voidBUILDPARTB () {//here's how to build PARTB code specifically}; Public voidBUILDPARTC () {//here's how to build PARTB code specifically}; PublicProduct GetResult () {//return to final assembly of finished product results}; } //built by Public classDirector {PrivateBuilder Builder; PublicDirector (Builder builder) { This. Builder =Builder; } Public voidconstruct () {Builder.buildparta (); BUILDER.BUILDPARTB (); BUILDER.BUILDPARTC (); }} Public InterfaceProduct {} Public InterfacePart {}
Newnew= Builder.getresult ();
From this point of view, the builder pattern integrates many functions into a class that can create more complex things. So the difference with the engineering model is that the factory model is concerned with creating a single product, while the builder pattern is concerned with creating a conforming object, multiple parts. Therefore, the choice of the factory model or the builder model depends on the actual situation.
Application Scenarios
This pattern is javamail used in Java applications.
Java design mode-builder mode (builder)