Design Pattern-builder Pattern

Source: Internet
Author: User

Design Pattern-builder Pattern

The builder mode is Separate the construction of a complex object from its representation so that the same construction process can create different representations. this means that the construction of a complex object is separated from its representation, so that different representations can be created during the same construction process.

The builder mode can be divided into the following four roles:

1. builder: provides an abstract interface that specifies the construction of each component of the product object.

2. ConcreteBuilder: implements all the methods in Builder. And return to the created instance.

3. Director: arranges the order of existing modules and then builds them through Builder.

4. Product: a complex object to be created. A system contains multiple Product classes. These Product classes do not necessarily have common interfaces, but are completely irrelevant.

The class diagram corresponding to the builder mode:

The code for each class is as follows:

Director

 

Package com. zz. builder;/***, for Assembly * @ author txxs **/public class Director {private Builder builder = new ConcreteBuilder (); // constructs a product, calls the construction method of each part. public Product builder () {builder. setPart1 (); builder. setPart2 (); builder. setPart3 (); // return builder for other parts. builderProduct ();}}

Abstract Builder

 

 

Package com. zz. builder;/*** abstract Builder * @ author txxs **/public abstract class builder {// set different parts of the product to obtain public abstract void setPart1 () of different products (); public abstract void setPart2 (); public abstract void setPart3 ();//...... Other parts // construction Product public abstract Product builderProduct ();}

Specific builder ConcreteBuilder:

 

 

Package com. zz. builder;/*** specific Builder to implement the inherited abstract Method * @ author txxs **/public class ConcreteBuilder extends builder {private Product product = new Product (); // set the product part @ Overridepublic void setPart1 () {// install the first part} @ Overridepublic void setPart2 () {// install the second part} @ Overridepublic void setPart3 () {// install Part 3 }//...... Other parts // build a Product @ Overridepublic product builderProduct () {return Product ;}}

Product

 

 

Package com. zz. builder;/*** Product class ** @ author txxs **/public class Product {// business processing method of the Product}

 

The following example uses the builder mode for application. In this example, the builder mode is used to simulate the assembly of mass CC. in the assembly process, mass CC is a product and is considered economical and luxurious, the economical and luxury engine models are different, and the luxury model has a car-mounted TV, while the economical model does not. The construction plant of mass CC is Builder, while the Assembly Workshop of different models implements ConcreteBuilder respectively, and finally runs scheduling Assembly through ctor. Download the source code. Running result

In what scenarios are the builder models used?

1. In the same method, different execution sequence and different results are generated.

2. Assemble multiple parts or parts into an object, but the result is different.

3. When the product type is complex, or the call sequence of different methods in the product type produces different results.

The difference between the Creator mode and the factory method in the same creation mode is that the Creator mode focuses on the type of parts and the assembly process.

 

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.