Building model builder

xiaoxiao2021-04-04  227

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 {// Construction personnel

// Creating a component A such as a car wheel vid buildparta (); // Creating part b, such as creating a car steering wheel void buildpartB (); // Creating a part C, such as creating a car engine void buildingc (); // Return the final assembly result ( Returns the last installed car) // The assembly process of the finished product is not performed here, but is transferred to the Director class below. // thus implementing the decoupling 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 composed of these components), that is, how the Director is finally assembled into finished products:

Public Class Director {// Supervisor Private Builder;

Public Director (builder builder) {this.builder = builder;} // Take part parta partb partc Finally Complicated Objects / / This is the process of loading the wheel steering wheel and the engine block into a car for a car. Public void construct () 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 {// Construction Part Parta, Partb, Partc; Public Void BuildParta () {// This is how to build parta code}; public void buildpartb () {// This is how to build a partb code }; Public void buildpartc () {// This is the code of how to build a partb}; 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 species in the builder model to diagnose "the limb", which part is broken, and then repair this part. (I can't understand this passage)

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

New Post(0)