Builder mode Definition: Separate a complex item with its representation, making the same build process can create different representations.
The Builder mode is a step-by-step creation of a complex object that allows users to build them only by specifying the type and content of complex items. The user does not know the details of the internal specific build. The Builder mode is very similar to abstract factory model, which is probably only in repeated use to experience.
Why is used? It is to decouple the process of constructing complex objects and its components. Note: It is a decoupling process and component.
Because of a complex object, there are not only a lot of components, such as cars, there are many parts: the wheel steering wheel also has various small parts, many parts, but far more than these, how to assemble these parts into a car, this assembly The process is also complex (requires a good assembly technology), and the Builder mode is to separate the components and assembly processes.
How to use? First, it is assumed that a complex item is composed of multiple components, and the Builder mode is the creation of complex objects and the creation of components, respectively, and is represented by the Builder category and the Director category.
First, you need a interface that defines how to create all parts of the complex object:
Public interface builder {// Create parts A, such as creating car wheel void BuildParta (); // Creating part b, such as creating car steering wheel void buildpartb (); // Creating part C, such as creating car engine void BuildPartc (); // Return the last The assembly result of the assembly (returning the last installed car) // The assembly process of the finished product is not performed here, but is transferred to the Director category below. / / Thus implementing the decoupling process and component product getResult ();
Using Director to construct the final complex item, how to create one component in the BUILDER interface (complex item is made of these components), that is, how the contents of the Director are finally assembled into finished products:
Public Class Director {Private Builder; Public Director (Builder Builder) {THIS. Builder = builder;} // Take the component Parta Partb PARTC Finally, the complex object / / This is the process of loading the wheel steering wheel and the engine assembly into a car for a car. Public void, (); builder.buildpartb (); builder. BuildPartc ();
The specific implementation of Builder Concretebuilder: Build or assembles components of the product by specific completion of the arch. It is defined and clear what specific things it wants; provides an interface that can re-acquire the product:
Public Class Concretebuilder Implements Builder {Part Part, Partb, Partc; Public Void BuildParta () {// This is how to build parta code}; public void buildpartb () {// This is a specific partb code}; public Void buildpartc () {// This is a specific partb code}; public product getResult () {// Returns the final assembly result};
Complex item: Products Product:
Public interface product {}
Components of complex objects:
Public interface part {}
We look at how to call Builder mode: ConcreteBuilder builder = new ConcreteBuilder (); Director director = new Director (builder); director.construct (); Product product = builder.getResult (); application Bridger mode of practical use in Java, We often use the "pool" concept, which requires the pool when the resource provider cannot provide sufficient resources, and these resources need to be used repeatedly in multiple users.
"Pool" is actually a memory. When there are some complex resources of resources (such as the connection pool of the database, there may be a connection to interruption), if the loop is re-uses these "broken limbs", The memory usage efficiency will increase the performance of the pool. Modifying the Director category in the Builder mode to diagnose "the fill" break on which part of the "Failure", and then fix this part.