Design Pattern-builder Pattern

Source: Internet
Author: User

Design Pattern-builder Pattern

Concept:

Builder mode separates the creation of a complex object from its representation, so that different representations can be created during the same construction process.

The builder model can separate the internal appearance of a product from the product generation process, so that a building process can generate product objects with different internal appearances. We only need to specify the types to be built to get them, and the specific construction process and details do not need to be known.

Example:

A fresh and simple example can make it easy to understand obscure concepts. Let's take a look at a car builder model.

We know that cars generally consist of tires, engines, and body. Like BMW, Ferrari and Benz, they are also made up of these three items, but the price of the modules is different.

For them, although the price of component modules is different, building cars all contain these three things, which is a public operation. Therefore, you can create an abstract parent car building class (CarBuilder) and declare three interfaces, including virtual void BuildWheel. Create three sub-classes of automobile construction: BMWBuilder, FerraliBuilder, and BenzBuilder, which are used to build a specific automobile and provide interfaces for returning specific automobile product objects. They inherit from CarBuilder, and each sub-class implements personalized construction of the parent class module.

Then define a car building instructor, CarDirector, to control the vehicle construction process and isolate the association between users and the construction process. Finally, define a Product type Product to display the final generated automotive Product.

Role:

1. Builder: Specifies an abstract interface for each part of a product object, such as CarBuilder.

2. ConcreteBuilder: implements the Builder interface to construct and assemble each part of the product, defines and clarifies the representation it creates, and provides an interface for obtaining finished vehicle objects, such as BMWBilder.

3. Director: constructs an object using the Builder interface to control the construction process and isolate the association between users and the construction process, such as CarDirector.

4. Product: a complex object constructed, such as a BMW car.

UMLFigure:

Code:

 

# Pragma warning (disable: 4786) # include
 
  
# Include
  
   
# Include
   
    
Using namespace std; // Product class Product {public: vector
    
     
St; public: void AddParts (string sValue) {st. push_back (sValue);} void Show () {for (vector
     
      
: Iterator stIter = st. begin (); stIter! = St. end (); stIter ++) {cout <* stIter <
      
        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 ---- <
       
         > ITag; if (iTag = 1) {BMWBuilder * bb = new BMWBuilder; CarDirector cd (bb); cd. createCar (); product = bb-> GetProduct (); product. show ();} else if (iTag = 2) {FerraliBuilder * fb = new FerraliBuilder; CarDirector cd (fb); cd. createCar (); product = fb-> GetProduct (); product. show ();} else if (iTag = 3) {BenzBuilder * bb = new BenzBuilder; CarDirector cd (bb); cd. createCar (); product = bb-> GetProduct (); product. show ();} cout <---- builder mode ends ---- <
        
         

 

Divergence:

We know that there are three methods to realize C ++ polymorphism: function overload, template function, and virtual function. The polymorphism implemented by virtual functions is called dynamic polymorphism. The above Code has the following features:

1. The subclass object is converted to the parent class object, such as CarBuilder * cb = new BMWBuilder, which is called upward transformation. It is secure and automatically completed, and information of child types will be lost;

2. To solve the problem of child type information loss (subclass object is converted to parent class), the parent class must implement a virtual function BuildWheel;

3. The subclass has the same BuildWheel function, which overwrites the virtual function BuildWheel of the parent class, so that dynamic polymorphism can be realized (otherwise, only pointers or references can be used ).

Application scenarios:

1. When some complex objects are created, the building sequence between these objects is usually stable, but the building inside the objects is subject to complex changes;

2. When the constructor must allow different representations of the constructed object.

Advantages:

The Construction Code is separated from the representation code. Because the builder hides how the product is assembled, if you need to change the internal representation of a product, you only need to define a specific builder to reduce coupling.

 

Related Article

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.