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)