Java Builder Mode (builder mode)

Source: Internet
Author: User

Builder mode Definition: separates the construction of a complex object from its representation so that the same build process can create different representations.

The builder pattern is a step-by-step creation of a complex object that allows the user to build them only by specifying the type and content of the complex objects. The user is not aware of the specifics of the build internals. The builder pattern is very similar to the abstract factory pattern, with subtle differences that can only be experienced in repeated use.

Why Use Builder Mode

is to decouple the process of building a complex object from its parts. Note: The decoupling process and components.

Because a complex object, not only has a lot of components, such as cars, there are many parts: wheels, Steering wheel, engine, and a variety of small parts and so on, many parts, but much more than these, how to assemble these parts into a car, the assembly process is also very complex (need good assembly technology), The builder mode is designed to separate parts from the assembly process.

How to Use builder mode

First, suppose a complex object is made up of multiple parts, and the builder pattern is to separate the creation of complex objects from the creation of parts, represented by the builder class and the Director class, respectively.

First, you need an interface that defines how to create individual parts of a complex object:

 Public InterfaceBuilder {//creating a part A such as creating a car wheel   voidBuildparta ();//creating a part B such as creating a car steering wheel   voidBUILDPARTB ();//creating a part C such as creating a car engine   voidBUILDPARTC ();//returns the final assembly of the finished product (back to the last assembled car)//the assembly process for the finished product is not carried out here, but instead is transferred to the following Director class.//thus realizing the understanding of the coupling process and componentsProduct GetResult ();}

The final complex object is built with the director, and in the builder interface above it is how to create parts (complex objects are made up of these parts), that is, how the director's content is finally assembled into a finished product:

 Public class Director {  private  builder Builder;  Public Director (Builder builder) {  this. Builder = Builder;} // Parta A part PartB PARTC the final composition of a complex object // This is the process of assembling the wheel steering wheel and the engine into a car.  Public void  construct () {Builder.buildparta ();  BUILDER.BUILDPARTB (); BUILDER.BUILDPARTC (); }} 

The concrete implementation of builder ConcreteBuilder:

    • To build or assemble parts of the product by completing the interface builder specifically;
    • Define and clarify what the specific thing it is to create;
    • Provides an interface that can be re-acquired for a product.
 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};}

Complex objects: Product Products:

     Public Interface Product {}

Parts of Complex objects:

     Public Interface Part {}

Let's see how to call the builder pattern:

Newnew= Builder.getresult ();

Application of Builder Mode

In Java practical use, we often use the concept of "pool", when the resource provider is unable to provide sufficient resources, and these resources need to be shared repeatedly by many users, it is necessary to use the pool.

"Pool" is actually a piece of memory, when there are some complex resources in the pool "limb" (such as the database connection pool, perhaps sometimes a connection will be interrupted), if the recycling of these "limbs", will improve the efficiency of memory use, improve the performance of the pool. Modify the Director class in the builder mode so that it can diagnose which part the "limb" is broken on, and then fix the part.

Java Builder Mode (builder mode)

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.