C + + design pattern Shallow knowledge builder mode

Source: Internet
Author: User
Builder mode: Separates the construction of a complex object from its representation so that the same build process can create different representations.

Pattern implementation:

[Code]class builder;//Product class product{public:void Addpart (const char *info) {m_partinfovec.push_back (info); } void Showproduct () {for (std::vector<const char *>::iterator item = M_partinfovec.begin (); Item! = M_par Tinfovec.end ();        ++item) {std::cout << *item << Std::endl; }}private:std::vector<const Char *> m_partinfovec;};/ /builder Builder, Unified abstract interface class builder{public:virtual void Buildparta () {} virtual void Buildpartb () {} virtual product* GetProduct () {return null;}};/    /Concrete Created Object method class Concretebuilder:public Builder{public:concretebuilder () {m_product = new Product ();}    void Buildparta () {M_product->addpart ("Parta completed");    } void Buildpartb () {m_product->addpart ("PartB Q");    } product* GetProduct () {return m_product; }private:product *m_product;};/    /director controls the creation of the concrete Construction object class Director{public:director (Builder *builder) {m_builder = builder;} void CreAteproduct () {M_builder->buildparta ();    M_BUILDER-&GT;BUILDPARTB (); }private:builder *m_builder;};

Client:

[Code]int Main () {    Builder *builderobj =  new ConcreteBuilder ();    Director directorobj (builderobj);    Directorobj.createproduct ();    Product *productobj = Builderobj->getproduct ();    if (productobj = = NULL)    {        return 0;    }    Productobj->showproduct ();    Delete productobj;    Productobj = NULL;    Delete builderobj;    Builderobj = NULL;}

The above is the C + + design mode of knowledge of the content of the builder model, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.