Design pattern-Abstract Factory mode (c + + implementation)

Source: Internet
Author: User

1#include <iostream>2#include <string>3 4 using namespacestd;5 6 7 classstabstractproducta8 {9  Public:Ten         Virtual voidUse () =0; One }; A  - classSTProductA1: Publicstabstractproducta - { the  Public: -         Virtual voidUse () -         { -cout<<"Use ProductA1 ......."<<Endl; +         } - }; +  A classSTProductA2: Publicstabstractproducta at { -  Public: -         Virtual voidUse () -         { -cout<<"Use ProductA2 ......."<<Endl; -         } in }; -  to classSTABSTRACTPRODUCTB + { -  Public: the         Virtual voidEat () =0; * }; $ Panax Notoginseng classSTProductB1: PublicSTABSTRACTPRODUCTB - { the  Public: +         Virtual voidEat () A         { thecout<<"eat ProductB1 ... and so on."<<Endl; +         } - }; $  $ classSTProductB2: PublicSTABSTRACTPRODUCTB - { -  Public: the         Virtual voidEat () -         {Wuyicout<<"eat ProductB2 ... and so on."<<Endl; the         } - }; Wu  - classstabstractfactory About { $  Public: -         Virtualstabstractproducta* createproducta () =0; -         Virtualstabstractproductb* CREATEPRODUCTB () =0; - }; A  + classStconcretefactorya: Publicstabstractfactory the { -  Public: $         Virtualstabstractproducta*createproducta () the         { the                 return NewSTProductA1; the         } the         Virtualstabstractproductb*CREATEPRODUCTB () -         { in                 return NewSTProductB1; the         } the }; About  the classStconcretefactoryb: Publicstabstractfactory the { the  Public: +         Virtualstabstractproducta*createproducta () -         { the                 return NewSTProductA2;Bayi         } the         Virtualstabstractproductb*CREATEPRODUCTB () the         { -                 return NewSTProductB2; -         } the }; the  the intMainintargcChar*argv[]) the { -stabstractfactory* FC =NewStconcretefactorya (); thestabstractproducta* PA = fc->createproducta (); thestabstractproductb* PB = fc->CREATEPRODUCTB (); the 94Pa->Use (); thePb->eat (); the  thestabstractfactory* FC2 =NewStconcretefactoryb ();98stabstractproducta* PA2 = fc2->createproducta (); Aboutstabstractproductb* PB2 = fc2->CREATEPRODUCTB (); - 101Pa2->Use ();102Pb2->eat ();103 104         return 0; the }106 ////////////////////////////////////////107[Email protected] ~/learn_code/design_pattern/9_abstract_factory]$.abstract_factory108 Use ProductA1 .......109 eat ProductB1 ... and so on. the Use ProductA2 .......111Eat ProductB2 ... and so on.

Design pattern-Abstract Factory mode (c + + implementation)

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.