Building model builder

xiaoxiao2021-03-06  72

Builder mode definition: Separate a complex object with its representation, making the same build process can create different representations.

The Builder mode is a one-step to create a complex object that allows users to build them only by specifying the types and content of complex objects. Users don't know the details of internal specific build. The Builder mode is very similar to the abstract factories, subtle differences It is probably only in reused in use.

Why is it used 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 a variety of small parts, many parts, but far more than this, how to assemble these parts into a car, this The assembly process is also very complex (requires good assembly technology), and the Builder mode is to separate components and assembly processes.

How to use? First, it is assumed that a complex object is composed of multiple components, and the Builder mode is the creation of complex objects and the creation of components, respectively, with the Builder class and the Director class.

First, you need an interface that defines how to create the components of complex objects:

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 Assembly results (return to the last installed car) // The assembly process of the finished product is not performed here, but transferred to the Director class below. // thus enabling the decippsument process and component product getResult ();

Build a final complex object with Director, and how to create one component in the Builder interface (complex object is made up of these components), that is, how the contents of the Director are finally assembled into finished products:

Public class director {Private Builder Builder; Public Director (builder builder) {this.builder = builder;} // Wheel part Parta Partb Partc Finally Complicated Objects / / This is a process of loading a wheel steering wheel and an engine block into a car for a car PUBLIC VOID CONSTRUCT () {Builder.buildparta (); builder.buildpartb (); builder.buildpartc ();}}

The specific implementation of Builder Concretebuilder: Build or assembles the parts of the product by specific completion of the interface Builder; definition and clarify what specific things it wants to create; provide 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 object: Product products:

Public interface product {}

Component 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 Builder mode of practical use in Java, We often use the "pool" concept, which requires the pool when the resource provider is unable to provide sufficient resources, and these resources need to be used by many users.

"Pool" is actually a memory. When there are some complex resources in the pool (such as the connection pool of the database, there may be a connection to interrupt), if the loop is reused, it will increase memory usage. Efficiency, improve the performance of the pool. Modify the Director type in the builder mode to diagnose "the fill" break on which part of the "Failure limbs", and then fix this part.

Specific English Articles See: Recycle Broken Objects in Resource Pools

转载请注明原文地址:https://www.9cbs.com/read-93085.html

New Post(0)