Five, builder mode--Create pattern

Source: Internet
Author: User

Builder mode: The internal representation of a product can be separated from the production process, allowing a construction process to produce a product object with different internal representations

Building the UML class diagram of the modal:

Class Diagram Relationship Description:

Builder role: An abstract interface is given to standardize the construction of each component of the Product object. Typically, this interface is independent of the application's business logic. The Product object is created directly in the model
(concretebuilder) role. The concrete builder class must implement the method required by this interface: one is the construction method and the other is the method of returning the result.


Concrete Builder (concrete Builder) Role: This role is a tightly related class of applications that create product instances under Application invocation. The main tasks that this role accomplishes include:

• Implement the interface provided by the Builder role to complete the process of creating a product instance step-by-step.
• Provide examples of the product once the construction process is complete.


Mentor (Director) Role: The class that holds this role invokes the concrete builder role to create the Product object. The director does not have the specific knowledge of the product class, the actual product class specific knowledge is the concrete builder object.


Product role: A product is a complex object in construction. The mentor role is the role that the client deals with. The Director role divides requests for client-created products into construction requests for individual parts, and then the requests

Delegate to the specific builder role. The concrete builder's role is to do concrete construction work, but it is not known to the client.

Example code:

    classProgram {Static voidMain (string[] args) {Director Director=NewDirector (); Builder Builder1=NewConcreteBuilder1 (); Builder Builder2=NewConcreteBuilder2 (); Director.            Construct (Builder1); Product P1=Builder1.            GetResult (); P1.            Show (); Director.            Construct (BUILDER2); Product P2=Builder2.            GetResult (); P2.            Show ();                       Console.readkey (); }    }    classDirector { Public voidConstruct (builder builder) {Builder.            Builderparta (); Builder.        BUILDERPARTB (); }    }    Abstract classBuilder {Abstract  Public voidBuilderparta (); Abstract  Public voidBUILDERPARTB (); Abstract  PublicProduct GetResult (); }    classProduct {ArrayList parts=NewArrayList ();  Public voidADD (stringPart ) {Parts.        ADD (part); }         Public voidShow () {Console.WriteLine ("Product Parts--------"); foreach(stringIteminchparts)            {Console.WriteLine (item); }        }    }    classConcretebuilder1:builder {PrivateProduct Product =NewProduct ();  Public Override voidBuilderparta () {product. ADD ("Parta"); }         Public Override voidBUILDERPARTB () {product. ADD ("PartB"); }         Public OverrideProduct GetResult () {returnproduct; }    }    classConcretebuilder2:builder {PrivateProduct Product =NewProduct ();  Public Override voidBuilderparta () {product. ADD ("Partx"); }         Public Override voidBUILDERPARTB () {product. ADD (" Party"); }         Public OverrideProduct GetResult () {returnproduct; }    }
View Code

The result of the code:

Build Mode Improvements

1. If only one concrete builder is needed in the system, the abstract builder can be omitted.

2, in the case of concrete builders only one, if the abstract builder role has been omitted, then can also omit the instructor
Role. Let the builder role play the role of mentor and builder.

Mode application situation

The builder mode should be used in the following situations:
1. Product objects that need to be generated have complex internal structures. 2. The properties of the product objects that need to be generated depend on each other, and the builder pattern can force the generation sequence. 3, in the object creation process will use some other objects in the system, these objects in the Product object creation process is not easy to obtain.
The following are the main effects of using the builder model:
1, the use of construction mode makes the internal appearance of the product can change independently. Using the builder mode allows the client not to know the details of the internal composition of the product. 2. Each builder is relatively independent and not related to the other builder. 3, the model built by the final product more easy to control

Five, builder mode--Create 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.