Design Pattern Builder

Source: Internet
Author: User

Builder mode definition:
Separates the construction of a complex object from its representation so that different representations can be created during the same construction process.

The builder mode is used to create complex objects step by step. It allows users to build complex objects by specifying the types and content of complex objects. the user does not know the specific internal build details. the builder mode is very similar to the abstract factory mode. Slight differences can be realized only after repeated use.

Why?
To build complex objectsProcessAnd itsPartsDecoupling. Note: decouplingProcessAndParts.

Because a complex object not only has many components, such as a car, but also many components: the wheel engine and various small components. There are many components, but far more than these, how to assemble these components into an automobile is also complicated (requires good assembly technology). The Builder mode is to separate the components from the assembly process.

How to use it?
Assume that a complex object is composed of multiple parts. The Builder mode separates the creation of complex objects from the creation of parts, which are represented by the builder class and the director class respectively.

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

Public interface builder {

// Create part A, for example, creating a Car Wheel
Void buildparta ();
// Create part B, for example, create a car steering wheel
Void buildpartb ();
// Create part C, for example, create a car engine
Void buildpartc ();

// Return the final assembly result (return the final assembled car)
// The assembly process of finished products is not carried out here, but transferred to the director class below.
// Achieves decouplingProcessAndParts
Product getresult ();

}

Build the final complex object with ctor, and encapsulate in the above builder interface how to create parts (complex objects are composed of these parts ), that is to say, director's content is how to finally Assemble parts into finished products:

Public class director {

Private Builder;

Public Director (Builder ){
This. Builder = builder;
}
// Combine part parta partb partc to form a complex object
// The process of assembling the wheel steering wheel and engine into a car
Public void construct (){
Builder. buildparta ();
Builder. buildpartb ();
Builder. buildpartc ();

}

}

Concretebuilder:
Build or assemble product components through specific interface builder;
Define and clarify what specific things it wants to create;
Provides an interface for getting products again:

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 (){
// Return the final assembly result
};

}

Complex Object: product:

Public interface product {}

Components of complex objects:

Public interface part {}

Let's take a look at how to call the builder mode:
Concretebuilder builder = new concretebuilder ();
Director dire= new director (builder );

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

Application of builder Mode
In actual use of Java, we often use the concept of "pool". When the resource provider cannot provide enough resources and these resources need to be shared by many users, you need to use the pool.

"Pool" is actually a piece of memory. When the pool has some complex "broken limbs" (such as the database connection pool, sometimes a connection is interrupted ), if you reuse these "broken limbs" cyclically, the memory usage efficiency will be improved and the pool performance will be improved. modify the ctor class in builder mode to diagnose the part on which the "disconnected" part is broken and repair the part again.

 

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.