The builder model of C + + implementation of design patterns

Source: Internet
Author: User
Tags include prepare

Problems to solve:

The object I created is more complex, and the member functions inside the object represent different instances with different implementations, in other words, the same object-building process can have different representations. For example, I went to eat crossing that day, they have different packages, the package contains the same kind of, have a bowl of rice noodles, a cold dish, a drink. But the 3 of different packages are not all the same. At this point, we can use the builder model.

Class Diagram Structure:

1. Builder (Builder) role: An abstract interface is given to standardize the construction of each component of a Product object. Generally speaking, this interface is independent of the business logic of the application. The specific builder (concrete Builder) role is created directly in the schema. The concrete builder class must implement the method required by this interface: one is the construction method and the other is the result return method. At this time is the rice noodle shop staff, in accordance with the requirements of the cashier to prepare specific packages, put the appropriate rice noodles, cold dishes and beverages.

2. The specific builder (concrete Builder) Role: This role is a tightly related class of applications that create product instances under application calls. The main tasks of this role include: Implement the interface provided by the builder role, and step through the process of creating the product instance. Provide an example of the product after the construction process is completed. is a specific set of staff to do a package.

3. Mentor (Director) Role: The class that holds the role calls the concrete builder role to create the Product object. Directors do not have specific knowledge of the product category, the specific knowledge of the actual product class is the specific builder object. The cashier, he knew what I wanted, and he told the noodle shop staff to prepare the package.

4. Product role: A product is a complex object in construction. The Mentor role is the client's role in dealing with. The Director role divides the client's request to create a product into a build request for each part, and then delegates those requests to the specific builder role. The concrete builder role is to do the concrete construction work, but is not known by the client. It's the last set of packages, and everything comes together.

Sample implementation:

CplusplusBuild.cpp:Defines the entry point for the console application. #include "stdafx.h" #include <vector> #include <string> #include <iostream> using NA  
Mespace std;  
    Product class Food {private:<strong> </strong>vector<string> mfoodname;  
      
Vector<int> Mfoodprice;        
        Public:void Add (String Foodname,int price) {mfoodname.push_back (foodname);  
    Mfoodprice.push_back (price);  
        } void Show () {cout<< "Food List" <<endl;  
        cout<< "-------------------" <<endl; for (int i=0;i<mfoodname.size (); i++) {cout<<mfoodname[i]<< "" <<mfoodprice[i]&  
        lt;<endl;  
      
}  
    }  
};  
    Builder class Builder {public:virtual void Buildricenoodles () {};  
    virtual void Buildcooldish () {};  
    virtual void Builddrink () {}; VIrtual Food * Getfood () {return NULL;}  
      
      
};  
      
Buildera class Buildera:public Builder {Private:food *food;  
    Public:buildera () {food = new food ();}  
    void Buildricenoodles () {Food->add ("Ricenoodlesa", 20);  
    } void Buildcooldish () {Food->add ("Cooldisha", 20);  
    } void Builddrink () {Food->add ("Drinka", 20);  
    } Food * Getfood () {return Food;  
      
}  
};  
Builderb class Builderb:public Builder {Private:food *food;  
    Public:builderb () {food = new food ();}  
    void Buildricenoodles () {Food->add ("Ricenoodlesb", 10);  
    } void Buildcooldish () {Food->add ("COOLDISHB", 10);  
    } void Builddrink () {Food->add ("drinkb", 10);  
    } Food * Getfood () {return Food;  
      
}  
}; Director class Foodmanager {pUblic:void Construct (Builder * Builder) {builder->buildricenoodles ();  
        Builder->builddrink ();  
    Builder->buildcooldish ();  
      
}  
};  
      
    clent int _tmain (int argc, _tchar* argv[]) {Foodmanager *foodmanager= new Foodmanager ();  
      
    Builder * Builder = new Builder ();  
    The following code can use simple factory;  
    Char ch;  
    cout<< "Input your food Type (A or B):";  
    cin>>ch;  
    if (ch== ' A ') {builder = new Buildera ();  
    }else if (ch== ' B ') {builder = new Builderb ();  
    } foodmanager->construct (builder);  
    Food * Food = Builder->getfood ();  
    Food->show ();  
return 0; }

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.