Design mode (-----) Builder mode

Source: Internet
Author: User

Builder Model Definition

Separating the construction of a complex object from its representation allows the same build process to create different representations.

UML diagram

  

Member Introduction

Abstract 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 direct creation of the Product object in the schema is the Concrete Builder (concretebuilder) role. The concrete builder class must implement two methods required by this interface: one is the construction method (BuildPart1 and BuildPart2) and the other is the return structure method (Retrieveresult). In general, the number of parts contained in the product corresponds to the number of construction methods. In other words, how many parts there are, how many corresponding construction methods.

  Concrete Builder (concretebuilder) role: This role is a class that is closely related to the application, which creates an instance of the product under the application invocation. The tasks to be accomplished by this role include: 1. Implement the interface declared by the abstract Builder builder, and give a step-by-step procedure for creating a product instance. 2. After the completion of the construction process, provide an example of the product.

  Director role: The class that holds the role invokes the concrete builder role to create the Product object. It should be noted that the Director's role does not have the specific knowledge of the product class, the real knowledge of the product class is the specific builder role.

  Product role: A product is a complex object in construction. In general, there will be more than one product class in a system, and these product classes do not necessarily have a common interface and can be completely unrelated.

Example

Builder class

 PackageCom.csdhsm.pattemdesign.builder;/*** @Title: Builder.java * @Description: Builder class *@author: Han * @date: June 23, 2016 PM 8:18:45*/   Public Abstract classBuilder { Public Abstract voidBuildparta ();  Public Abstract voidBUILDPARTB ();  Public AbstractProduct GetResult ();}

Concrete Builder Class

 PackageCom.csdhsm.pattemdesign.builder;/*** @Title: Concretebuilder1.java * @Description: Concrete Construction 1 *@author: Han * @date: June 23, 2016 PM 8:19:23*/   Public classConcreteBuilder1extendsBuilder {PrivateProduct Product =NewProduct (); @Override Public voidBuildparta () {Product.add ("Part A"); } @Override Public voidBUILDPARTB () {Product.add ("Part B"); } @Override PublicProduct GetResult () {returnproduct; }}
 PackageCom.csdhsm.pattemdesign.builder;/*** @Title: Concretebuilder2.java * @Description: Concrete Construction 2 *@author: Han * @date: June 23, 2016 PM 8:19:40*/   Public classConcreteBuilder2extendsBuilder {PrivateProduct Product =NewProduct (); @Override Public voidBuildparta () {Product.add ("Part X"); } @Override Public voidBUILDPARTB () {Product.add ("Part Y"); } @Override PublicProduct GetResult () {returnproduct; }}

Conductor class

 Package Com.csdhsm.pattemdesign.builder; /**    * @Title:  director.java    @author: Han    * @date:   June 23, 2016 8:20:06    */   Public class Director {    publicvoid  construct (builder builder) {        Builder.buildparta ();        BUILDER.BUILDPARTB ();    }}

Product Category

 PackageCom.csdhsm.pattemdesign.builder;Importjava.util.ArrayList;Importjava.util.List;/*** @Title: Product.java * @Description: Product class *@author: Han * @date: June 23, 2016 PM 8:20:30*/   Public classProduct {List<String> parts =NewArraylist<>();  Public voidAdd (String part) {Parts.add (part); }         Public voidShow () {System.out.println ("Product Creation----");  for(String part:parts) {System.out.println (part); }    }}

Client

 PackageCom.csdhsm.pattemdesign.builder; Public classSolution { Public Static voidMain (string[] args) {Director Director=NewDirector (); Builder B1=NewConcreteBuilder1 (); Builder B2=NewConcreteBuilder2 ();        Director.construct (B1); Product P1=B1.getresult ();                P1.show ();        Director.construct (B2); Product P2=B2.getresult ();    P2.show (); }}
Summarize the circumstances under which construction patterns are used

1. The product objects that need to be generated have complex internal structures, each of which can itself be an object or simply an integral part of an object (that is, a product object).

2. The properties of the product objects that need to be generated depend on each other. The construction mode can enforce a step-through construction process, so if one property of a Product object has to be assigned after another attribute is assigned, using the build mode is a good idea.

3. Some other objects in the system are used during object creation, which are not readily available during the creation of the Product object.

Design mode (-----) Builder mode

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.