Java design mode-builder mode (builder)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.