First, the relevant introduction
The abstract factory pattern provides an interface to create a series of related or interdependent objects without specifying their specific classes.
Second, UML diagram
Third, C + + programs
1 //abstract Factory mode. CPP: The entry point that defines the console application. 2 //3 4#include"stdafx.h"5#include <iostream>6 using namespacestd;7 8 //Linux Series9 classLinuxTen { One Public: A Linux () {} - Virtual~Linux () {} - Virtual voidShow_price () =0; the }; - - //Linux Phones - classPlinux: PublicLinux + { - Public : + Plinux () A { atcout<<"I am a Linux phone"<<Endl; - } - Virtual~Plinux () {} - Virtual voidShow_price (); - }; - in voidPlinux::show_price () - { tocout<<"my Price is"<<Endl; + } - //Linux Computers the classPclinux: PublicLinux * { $ Public :Panax Notoginseng Pclinux () - { thecout<<"I am a Linux PC"<<Endl; + } A Virtual~Pclinux () {} the Virtual voidShow_price (); + }; - voidPclinux::show_price () $ { $cout<<"my Price is"<<Endl; - } - the //Windows Products - classWindowsWuyi { the Public: - windows () {} Wu Virtual~windows () {} - Virtual voidShow_price () =0; About }; $ - - //Windows Phone - classPwindows: PublicWindows A { + Public : the pwindows () - { $cout<<"I am a Windows Phone"<<Endl; the } the Virtual~pwindows () {} the Virtual voidShow_price (); the }; - in voidPwindows::show_price () the { thecout<<"My Price is 1999"<<Endl; About } the the //Windows PC the classPcwindows: PublicWindows + { - Public : the pcwindows ()Bayi { thecout<<"I am a Windows PC"<<Endl; the } - Virtual~pcwindows (); - Virtual voidShow_price (); the }; the the voidPcwindows::show_price () the { -cout<<"my Price is 4999"<<Endl; the } the //Factory abstract class the classFactory94 { the Public: the Factory () {} the Virtual~Factory () {}98 VirtualLinux * creatlinux () =0; About VirtualWindows*creatwindows () =0; - };101 //Mobile phone Factory102 classPhonefactory: PublicFactory103 {104 Public: the phonefactory () {}106 Virtual~phonefactory () {}107 VirtualLinux *Creatlinux ()108 {109 return NewPlinux (); the }111 VirtualWindows *creatwindows () the {113 return Newpwindows (); the } the }; the //Computer Factory117 classPcfactory: PublicFactory118 {119 Public: - pcfactory () {}121 Virtual~pcfactory () {}122 VirtualLinux *Creatlinux ()123 {124 return NewPclinux (); the }126 VirtualWindows *creatwindows ()127 { - return Newpcwindows ();129 } the };131 the //user Interface133 int_tmain (intARGC, _tchar*argv[])134 {135Factory *phone=NULL;136Phone=Newphonefactory;137Linux *phone_linux=phone->Creatlinux ();138Phone_linux->Show_price ();139Windows *phone_windows=phone->creatwindows (); $Phone_windows->Show_price ();141 142 return 0;143}
Abstract Factory mode