Builder mode of design pattern

Source: Internet
Author: User

Builder mode Definition: separates the construction of a complex object from its representation so that the same build process can create different representations.

The builder pattern is a step-by-step creation of a complex object that allows the user to build them only by specifying the type and content of the complex objects. The user is not aware of the specifics of the build internals. The builder pattern is very similar to the abstract factory pattern, with subtle differences that can only be experienced in repeated use.

Why Use Builder Mode

is to decouple the process of building a complex object from its parts. Note: The decoupling process and components.

Because a complex object, not only has a lot of components, such as cars, there are many parts: wheels, Steering wheel, engine, and a variety of small parts and so on, many parts, but much more than these, how to assemble these parts into a car, the assembly process is also very complex (need good assembly technology), The builder mode is designed to separate parts from the assembly process.

How to Use builder mode

First, suppose a complex object is made up of multiple parts, and the builder pattern is to separate the creation of complex objects from the creation of parts, represented by the builder class and the Director class, respectively.

First, you need an interface that defines how to create individual parts of a complex object:

 Public InterfaceBuilder {//creating a part A such as creating a car wheel   voidBuildparta ();//creating a part B such as creating a car steering wheel   voidBUILDPARTB ();//creating a part C such as creating a car engine   voidBUILDPARTC ();//returns the final assembly of the finished product (back to the last assembled car)//the assembly process for the finished product is not carried out here, but instead is transferred to the following Director class.//thus realizing the understanding of the coupling process and componentsProduct GetResult ();}

The final complex object is built with the director, and in the builder interface above it is how to create parts (complex objects are made up of these parts), that is, how the director's content is finally assembled into a finished product:

 Public class Director {  private  builder Builder;  Public Director (Builder builder) {  this. Builder = Builder;} // Parta A part PartB PARTC the final composition of a complex object // This is the process of assembling the wheel steering wheel and the engine into a car.  Public void  construct () {Builder.buildparta ();  BUILDER.BUILDPARTB (); BUILDER.BUILDPARTC (); }} 

The concrete implementation of builder ConcreteBuilder: * To build or assemble parts of the product by completing the interface builder specifically; * Define and clarify what specific things it is going to create; * provides an interface to regain the product.

 Public classConcreteBuilderImplementsBuilder {Part parta, PartB, PARTC; Public voidBuildparta () {//here's how to build Parta code specifically};  Public voidBUILDPARTB () {//here's how to build PARTB code specifically};  Public voidBUILDPARTC () {//here's how to build PARTB code specifically};  PublicProduct GetResult () {//return to final assembly of finished product results};}

Complex objects: Product Products:

 Public Interface Product {}

Parts of Complex objects:

 Public Interface Part {}

Let's see how to call the builder pattern:

Newnew= Builder.getresult ();

As we can see from the above, the benefit of the builder pattern is to separate the construction code from the presentation code, as the builder hides how the product is assembled, so if you need to change the internal representation of a product, you just need to define a concrete builder.

Builder mode of design pattern

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.