6th creation Mode-builder mode

Source: Internet
Author: User

1. definition of builders (builder, or generator) mode

(1) Separating a complex object construct from its representation so that the same build process can create different representations.

① the above mentioned " build " refers to the construction process of the algorithm (that is, the construction sequence, in the Director class), " representation " refers to the specific details of the generated parts (or called implementation, In the builder or its subclasses).

The ② is guided by the mentor (director) to guide the construction process , and the builder is responsible for the specific implementation of each step of the object and Assemble the parts .

Instructors can reuse The build process , and the generator is a concrete implementation that can be switched.

(2) Structure and description of the builder model

①builder: The Builder interface, which defines the interfaces for each part needed to create a product object.

②concretebuilder: A concrete builder implementation that implements the creation of individual components and is responsible for assembling the individual parts of the Product object, as well as providing a way for the user to obtain a Product object after the assembly is completed.

③director: Instructor, mainly used to build the required product objects in a unified year process using the builder interface.

④product: A product that represents a complex object built by a builder that contains multiple parts.

2. Thinking of the builder model

(1) The function of the builder mode : To build complex products, and to build products in a refined and step-up way. That is, it is one step at a point to solve the problem of constructing complex objects.

The process of building the ① is uniform and fixed (then the instructor formulates). Change is the concrete implementation of each step, which is implemented by the builders.

The focus of the ② Builder pattern is on the separation of construction algorithms and the implementation of concrete constructs .

(2) Composition of the builder model--two parts

  ①builder Interface : This defines how each part is built, that is, how the functionality of each part is implemented, and how to assemble the parts into the product. that is, part construction and product assembly .

  ②director: Knowing what process to build the product, responsible for the overall build algorithm, is usually done in steps . The emphasis here is that the overall build algorithm is fixed. when the director implements the overall build algorithm , when it encounters the need to create and assemble specific components , these specific implementations are delegated to the Builder to complete .

"Programming experiment" to build Shenzhou spacecraft

//Create pattern: Builder Mode//Assembly of Shenzhou spacecraft#include <stdio.h>#include<string>using namespacestd;//************************* Auxiliary class: various parts ************************//Orbital Moduleclassorbitalmodule{Private:    stringname; Public: Orbitalmodule (stringname) {         This->name =name; }        voidSetName (stringname) {         This->name =name; }        stringGetName () {returnname;};//engineclassengine{Private:    stringname; Public: Engine (stringname) {         This->name =name; }        voidSetName (stringname) {         This->name =name; }        stringGetName () {returnname;};//Escape Towerclassescapetower{Private:    stringname; Public: Escapetower (stringname) {         This->name =name; }        voidSetName (stringname) {         This->name =name; }        stringGetName () {returnname;};//Final Productclassairship{Private: Orbitalmodule* Orbitalmodule;//Orbital Moduleengine* engine;//engineescapetower* Escapetower;//Escape Tower Public: Orbitalmodule* Getorbitalmodule () {returnOrbitalmodule;} voidSetorbitalmodule (orbitalmodule*orbitalmodule) {         This->orbitalmodule =Orbitalmodule; } Engine* Getengine () {returnengine;} voidSetengine (engine*engine) {         This->engine =engine; } escapetower* Getescapetower () {returnEscapetower;} voidSetescapetower (escapetower*escapetower) {         This->escapetower =Escapetower; }    voidlaunch () {//detects if the engine is normalprintf"%s\n",engine->getName (). C_STR ()); //detects if the orbital compartment is normalprintf"%s\n",orbitalmodule->getName (). C_STR ()); //detects if the escape tower is normalprintf"%s\n",escapetower->getName (). C_STR ()); //Launchprintf"launch...\n"); }    };//*******************************************builder: Builders ' *****************************//Abstract Builderclassairshipbuilder{ Public:    //Build the engine    Virtual voidBuilderengine () =0; //building an orbital module    Virtual voidBuilderorbitalmodule () =0; //Building Escape Towers    Virtual voidBuilderescapetower () =0;};//Concrete BuildersclassConcreteairshipbuilder: Publicairshipbuilder{Private: Airship Airship; Public:    //construction of engine parts    voidBuilderengine () {Engine* Engine =Airship.getengine (); if(Engine! =NULL)Deleteengine; Engine=NewEngine ("Airship ' s engine!"); //Assembly WorkAirship.setengine (engine); }        //construction of Orbitalmodule parts    voidBuilderorbitalmodule () {Orbitalmodule* Orbitalmodule =Airship.getorbitalmodule (); if(Orbitalmodule! =NULL)DeleteOrbitalmodule; Orbitalmodule=NewOrbitalmodule ("Airship ' s orbitalmodule!"); //Assembly WorkAirship.setorbitalmodule (Orbitalmodule); }        //construction of Escapetower parts    voidBuilderescapetower () {Escapetower* Escapetower =Airship.getescapetower (); if(Escapetower! =NULL)DeleteEscapetower; Escapetower=NewEscapetower ("Airship ' s escapetower!"); //Assembly WorkAirship.setescapetower (Escapetower); }        //return to the final product (Shenzhou spacecraft)airship& GetResult () {returnairship;} ~Concreteairshipbuilder () {Orbitalmodule* Orbitalmodule =Airship.getorbitalmodule (); if(Orbitalmodule! =NULL)DeleteOrbitalmodule; Engine* Engine =Airship.getengine (); if(Engine! =NULL)Deleteengine; Escapetower* Escapetower =Airship.getescapetower (); if(Escapetower! =NULL)DeleteEscapetower; }};//********************************director: Mentor ******************************classdirector{Private: Airshipbuilder*Builder; Public: Director (Airshipbuilder*builder) {         This->builder =Builder; }        //Build Process//1, the builders of the construction of the various parts can be sequential, but the order of this example is not obvious or insignificant. //2, in this class only the process of construction (process, also the algorithm), the assembly process is placed in the builder class.     voidconstruct () {//1. Build the engine firstBuilder->Builderengine (); //2. Building the orbital module againBuilder->Builderorbitalmodule (); //3. Finally build the escape towerBuilder->Builderescapetower (); }};intMain () {//Client Invocation Example//build byairshipbuilder* Builder =NewConcreteairshipbuilder (); //MentorDirector* Director =NewDirector (builder); Director->construct ();//Build the final product//Testairship& Airship = ((concreteairshipbuilder*) builder)GetResult ();          Airship.launch (); DeleteBuilder; DeleteDirector; return 0;}

6th creation 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.