C + + Design pattern for abstract Factory mode

Source: Internet
Author: User
Abstract Factory: Provides an interface to create a series of related or interdependent objects without specifying their specific classes.

Pattern implementation:

[Code]//create productaclass producta{public:virtual void Show () = 0;};    Class Producta1:public Producta{public:void Show () {std::cout << "I m producta1\n";    }};class producta2:public producta{public:void Show () {std::cout << "I m producta2\n"; }};//create productbclass productb{public:virtual void Show () = 0;};    Class Productb1:public Productb{public:void Show () {std::cout << "I m productb1\n";    }};class productb2:public productb{public:void Show () {std::cout << "I m productb2\n";    }};class factory{public:virtual producta *createproducta () = 0; Virtual PRODUCTB *CREATEPRODUCTB () = 0;};    Class Factory1:public Factory{public:producta *createproducta () {return new ProductA1 ();    } PRODUCTB *CREATEPRODUCTB () {return new ProductB1 ();    }};class factory2:public factory{public:producta *createproducta () {return new ProductA2 ();     } PRODUCTB *CREATEPRODUCTB () {   return new ProductB2 ();        }};template <typename t>void del (t* obj) {if (obj! = NULL) {Delete obj;    obj = NULL; }}

Client:

[Code]int Main () {    Factory *factoryobj1 = new Factory1 ();    ProductA *productobja1 = Factoryobj1->createproducta ();    PRODUCTB *productobjb1 = FACTORYOBJ1->CREATEPRODUCTB ();    Productobja1->show ();  Output:i ' m ProductA1    productobjb1->show ();  Output:i ' m ProductB1    Factory *factoryobj2 = new Factory2 ();    ProductA *productobja2 = Factoryobj2->createproducta ();    PRODUCTB *productobjb2 = FACTORYOBJ2->CREATEPRODUCTB ();    Productobja2->show ();  Output:i ' m ProductA2    productobjb2->show ();  Output:i ' m ProductB2    del (productObjB2);    Del (productObjA2);    Del (FACTORYOBJ2);    Del (productObjB1);    Del (productObjA1);    Del (FACTORYOBJ1);    return 0;}

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

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