Builder mode definition
:
Separates the construction of a complex object from its representation, so that the same construction process can be
To create a different representation.
The builder mode is used to create a complex object step by step.
Users can build complex objects only by specifying the types and content of complex objects. users do not know the specific internal construction details. The Builder mode is very similar to the abstract factory mode. The slight differences are roughly
It can be realized only when it is used repeatedly.
Why?
To build complex objectsProcess
And
ItsParts
Decoupling. Note: decouplingProcess
AndDepartment
Parts
.
Because a complex object not only has a lot of components
Parts, such as automobiles, have many components: wheel steering wheel
The engine also has a variety of small parts and so on, there are many parts, but far more than this, how to assemble these parts into a car, the assembly process is also very complicated (requires a good assembly technology), the builder mode is
This is to separate parts from the assembly process.
How to use it?
First, assume that a complex object is composed of multiple components. The Builder Mode
Is to separate the creation of complex objects and the creation of parts, respectively expressed by the builder class and director class.
First, you need an interface that defines how to create complex
Each part of the object:
Public Interface builder {
// Create part A, for example, creating a Car Wheel Void buildparta (); // Create Part B, for example, creating 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, while Is transferred to the following ctor class. // Achieves decouplingProcess
AndDepartment Parts
Product getresult ();
}
|
Build the final complex object with ctor,
The above builder interface encapsulates how to create components (complex objects are composed of these components), that is, how the director content finally assembles components 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 ();
}
}
|
Implementation of builder
Concretebuilder:
Build or assemble product components through specific interface builder;
Define and clarify what specific things it wants to create;
Submit
For an interface that can obtain the product 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:
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 ();
Builder Mode
Use
In actual use of Java, we often use the concept of "pool ".
The source provider cannot provide enough resources, and these resources need to be shared by many users repeatedly.
"Pool" is actually a piece of memory, when the pool has some complex resources "broken
(For example, a database connection pool may interrupt a connection sometimes). If you reuse the "disconnected" function cyclically, the memory usage efficiency will be improved and the pool performance will be improved. Modify the builder mode.
The director class enables you to diagnose the part on which the "disconnected" part is broken and repair the part again.
For more information, see
:
Recycle broken objects in resource pools