Builder Pattern)

Source: Internet
Author: User

1. Concept

Separates a complex build from its representation so that different representations can be created during the same build process. [Construction and representation separation, different representations are constructed in the same way]

Difference from Abstract Factory: In the builder mode, 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 mode can enforce a step-by-step construction process.

The construction mode encapsulates complex internal creation in the Internal. For external callers, they only need to pass in the builder and construction tool. The caller does not need to care about how the internal creation is finished.

For example, there are many components in a car, such as many parts, wheels, steering wheel, engine, and other small parts. But how can we assemble these components into a car, this assembly process is also very complex (requires good assembly technology). The Builder mode is to separate parts from assembly.

2. UML diagram

3.Code

     Public   Interface Builder {
Void Buildparta ();
Void Buildpartb ();
Void Buildpartc ();

Product getresult ();
}

// Specific construction tool
Public Class Concretebuilder Implements Builder {
Part parta, partb, partc;

Public Void Buildparta (){
// Here is how to build the parta code
};
Public Void Buildpartb (){
// Here is how to build the partb code
};
Public Void Buildpartc (){
// Here is how to build the partb code
};
Public Product getresult (){
// Returns the final assembly result.
};
}

// Builder
Public Class Director {
Private Builder;

Public Director (Builder ){
This . Builder = builder;
}
Public Void Construct (){
Builder. buildparta ();
Builder. buildpartb ();
Builder. buildpartc ();
}
}


Public Interface Product {}
Public Interface Part {}
The following describes how to call builder:
Concretebuilder =NewConcretebuilder ();
Director dire=NewDirector (builder );

Director. Construct ();
Product = builder. getresult ();

4. Application scenarios

This mode is used in Java applications.

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.