Java design pattern Builder (Transfer)

xiaoxiao2021-03-06  63

Builder mode definition: Separate a complex object with its representation, making the same build process can create a different representation. The Builder mode is a complex object step by step, which allows users to only specify the type of complex objects and The content can be built. Users don't 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. Why is it used? To build a complex object process and it Parts decoupling. Note: It is a decoupling process and component. Because of a complex object, not only have a lot of components, such as cars, there are many parts: the wheel steering wheel engine also has various parts, many parts, but More than this, how to assemble these parts into a car, this assembly process is also complex (requires good assembly technology), the Builder mode is to separate the components and assembly processes. How to use? First, a complex object is more The components are composed, and the Builder mode is to separateize the creation of complex objects and the creation of components, separately with the Builder class and the Director class. First, you need an interface that defines how to create complex objects: Public Interface Builder {// Creating parts a, such as creating car wheel void BuildParta (); // Creating part b, such as creating a car steering wheel void buildpartB (); // Create a part C, such as creating a car engine void building, (Returns the last installed car) // The assembly process of the finished product is not performed here, but transferred to the Director class below. // thus realize the decippsu process and component product getResult ();} Build the last use of Director Complex objects, and how to create one part in the Builder interface (complex object is composed of these components), that is, how the Director's content is assembled into finished products: public class director {Private Builder Builder Public Director (Builder Builder) {this.builder = builder;} // Put the part parta partb partc Finally Complicated Objects / / This is the process of loading the wheel steering wheel and the engine into a car for a car. Public void construct () {Builder.Buil DPARTA (); builder.buildpartb (); builder.buildpartc ();}}}} Builder concrete Concretebuilder: Build or assemble products by specific completion interface Builder; definition and clarify what specific things it wants to create; An interface that reaches the product: public class concretebuilder imports builder {part parta, partb, partc; public void buildparta () {// This is the code of how to build parta}; public void buildpartb () {// This is specific How to build partb code}; public void buildpartc () {// This is a specific partb code}; public product getResult () {// Return the final assembly result};} Complex object: Product PRODUCT: PUBLIC Interface Product {} part of a complex object: public interface part {} we see how to invoke mode Builder: ConcreteBuilder builder = new ConcreteBuilder (); Director director = new Director (builder); director.construct (); Product product = builder.getResult ( );

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

New Post(0)