Design patterns that's something--builder mode

Source: Internet
Author: User

concept :

Builder mode , which separates the creation of a complex object from its representation, so that the same build process can create different representations.

The builder model separates the internal representation of a product from the production process, allowing a construction process to produce objects with different internal representations. We can get them only by specifying the types we need to build, and we don't need to know the specifics of the construction process and details.

Example :

A vivid and simple example always makes it easy to understand obscure concepts. Let's take a look at a builder model for building cars.

we know that cars are generally made up of tires, engines and bodies. Just like BMW,Ferrari (Ferrali) and Mercedes-Benz (Benz) Three cars, they are made up of three kinds of things, but the composition of the module price is different.

For them, although the composition of the module price is different, but the construction of the car contains these three kinds of things, is a public operation. As a result, you can create an abstract father carBuilder, which declares three building functions such as the virtual void Buildwheel interface. Then create three specific car construction subcategories: Bmwbuilder,ferralibuilder and Benzbuilder, which are used for the construction of specific cars and provide an interface to return specific automotive product objects. They inherit from Carbuilder, each of which implements the personalization of the parent class module.

Then define a building vehicle mentor, Cardirector, to control the car construction process and isolate the connection between the user and the construction process. Finally, define a product class productsthat will be used to display the final generated car product.

role :

1. Builder: Specify an abstract interface for each part that creates a product object, such as carbuilder.

2.Concrete Builder (concretebuilder): Implement Builder interface to construct and assemble each part of the product, Defines and clarifies the representation it creates, providing an interface to get the car's finished object, such as bmwbilder.

3.directed leader (Director): Constructs an object that uses the builder interface to control the construction process and isolate the connection between the user and the construction process, such as Cardirector.

4.specific product: A complex object, such as a BMW car, that is constructed .

UML Figure :


Code:

#pragma warning (disable:4786) #include <iostream> #include <vector> #include <string>using namespace std;//specific product classes class Product{public:vector<string> St;public:void addparts (string svalue) {st.push_back (sValue);} void Show () {for (Vector<string>::iterator stiter=st.begin (); Stiter!=st.end (); stiter++) {Cout<<*stiter <<endl;}}};/ /builder class carbuilder{public:virtual void Buildwheel () {cout<< "Carbuilder:bulid wheel." <<endl; }virtual void Buildengine () {cout<< "Carbuilder:bulid engine." <<endl; }virtual void Buildbody () {cout<< "carbuilder:bulid body." <<endl; }};//specific builder class for building specific products//implementing Carbuilder interfaces to construct and assemble individual parts of the product, define and clarify the representations it creates, and provide an interface class for retrieving products Bmwbuilder:public Carbuilder{public:product product;public:void Buildwheel () {Product. Addparts ("Bmw:bulid wheel."); void Buildengine () {product. Addparts ("Bmw:bulid engine.");} void Buildbody () {product. Addparts ("Bmw:bulid body.");} Product GetProduct () {return product;}}; Class FerralibuildeR:public carbuilder{public:product product;public:void Buildwheel () {Product. Addparts ("Ferrali:bulid wheel."); void Buildengine () {product. Addparts ("Ferrali:bulid engine.");} void Buildbody () {product. Addparts ("Ferrali:bulid body.");} Product GetProduct () {return product;}}; Class Benzbuilder:public carbuilder{public:product product;public:void Buildwheel () {Product. Addparts ("Benz:bulid wheel."); void Buildengine () {product. Addparts ("Benz:bulid engine.");} void Buildbody () {product. Addparts ("Benz:bulid body.");} Product getproduct () {return product;}};/ /conductor, for controlling the construction process, isolating the user from the construction Process Association Class cardirector{private:carbuilder* Cb;public:cardirector (carbuilder* cb) {THIS-&GT;CB = CB;} ~cardirector () {if (cb!=null) {Delete CB;CB = NULL;}} void Createcar () {cb->buildwheel (); Cb->buildengine (); Cb->buildbody ();}}; int main () {int itag=0; Product product;cout<< "----Builder mode start----" <<endl;cout<< "Bmw:1,ferrali:2,benz:3" <<endl;cout << "Please enter the car you want to build:"; Cin>>itag;if (itag==1) {bmwbuilder* bb= new Bmwbuilder; Cardirector CD (BB); Createcar ();p roduct = bb->getproduct ();p roduct. Show ();} else if (itag==2) {ferralibuilder* fb= new Ferralibuilder; Cardirector CD (FB); Createcar ();p roduct = fb->getproduct ();p roduct. Show ();} else if (itag==3) {benzbuilder* bb= new Benzbuilder; Cardirector CD (BB); Createcar ();p roduct = bb->getproduct ();p roduct. Show ();} cout<< "----Builder mode End----" <<endl;return 1;}

divergence :

We know there are three ways to implement C + + polymorphism: function overloading, template functions, and virtual functions. virtual function Implementation of polymorphism called Dynamic polymorphism, the above code has the following characteristics:

1.objects of subclasses are converted to objects of the parent class such as carbuilder* cb= new Bmwbuilder, which we call upward transformation. It is secure, auto-completed, and will lose the subtype of information;

2, in order to solve the problem that the subtype information is lost (the subclass object is converted to the parent class), the parent class must implement a virtual function buildwheel;

3.subclasses have exactly the same buildwheel function, overriding the virtual function Buildwheel that overrides the parent class , so that dynamic polymorphism can be achieved (otherwise, only with pointers or references).

Application Scenarios :

1, when creating some complex objects, the construction order of these objects is usually stable, but the construction of the object inside faces complex changes;

2, when the construction process must allow the constructed object to have different representations.

Advantages :

Separating the construction code from the presentation code, as the builder hides how the product is assembled, so if you need to change the internal representation of a product, you only need to define a concrete builder to reduce the coupling.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Design patterns that's something--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.