First, UML diagram
Second, the concept
Builder Mode: separates the construction of a complex object from its representation so that the same build process can create different representations.
Third, the description
Role:
(1) Builder: is the abstract interface specified for each part of the product object that is created.
(2) ConcreteBuilder: is the concrete creator, implements the builder interface, constructs and assembles each part.
(3) Product: the specific product role.
(4) Director: conductor, he is building an object that uses the builder interface.
Q: When do I use builder mode?
A: It is primarily used to create complex objects in which the order in which they are built is usually stable, but the construction of objects within them often faces complex changes.
Q: What are the benefits of using the builder model?
A: The benefit of the builder model is to separate the construction code from the presentation code, since 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.
Iv. implementation of C + +
(1) Builder.h
#ifndef builder_h#define builder_h#include <string> #include <vector> #include <iostream>//product class, Product category, has a number of components. Class product{private:std::vector<std::string> parts;public://Add product parts void Add (std::string part) {Parts.push_ Back (part);} List all product parts void Show () {std::cout<< "Product creation------" <<std::endl;std::vector<std::string>::iterator It;for (It=parts.begin (); It!=parts.end (); it++) {std::cout<<*it<<std::endl;}}};/ /builder, abstract builder class builder{public:virtual void Buildparta () =0;virtual void Buildpartb () =0;virtual product* GetResult () =0;};/ /concretebuilder1, Concrete builder class Concretebuilder1:public builder{private:product* Product;public:concretebuilder1 () { Product=new Product ();} ~concretebuilder1 () {delete product;} void Buildparta () {Product->add ("Part A");} void Buildpartb () {Product->add ("Part B"); product* GetResult () {return product;}};/ /concretebuilder2, Concrete builder class Concretebuilder2:public builder{private:product* Product;public:concretebuilder2 () { Product=new Product ();} ~concretebuilder2 () {delete product;} void Buildparta () {Product->add ("part X");} void Buildpartb () {Product->add ("part Y"); product* GetResult () {return product;}};/ /director class, conductor category. Class Director{public:void Construct (builder* Builder) {Builder->buildparta (); BUILDER->BUILDPARTB ();}}; #endif
(2) Client.cpp
#include "Builder.h" #include <iostream> #include <cstdlib>//client, the customer does not know the specific construction process. void Main () {director* director=new Director (); builder* builder1=new ConcreteBuilder1 (); builder* builder2=new ConcreteBuilder2 ();std::cout<< "conductors build Products with ConcreteBuilder1 method:" <<std::endl; Director->construct (Builder1); product* P1=builder1->getresult ();p 1->show ();std::cout<<std::endl;std::cout<< " The conductor constructs the product by means of ConcreteBuilder2: "<<std::endl;director->construct (Builder2); product* P2=builder2->getresult ();p 2->show (); Std::cout<<std::endl;delete derector;delete Builder1; Delete Builder2;system ("pause");}
(3) Operation result
Big talk design pattern C + + Implementation-13th chapter-Builder mode