Design Pattern-abstract factory pattern (AbstactFactory)
Abstract Factory Model
The abstract factory model is actually implemented through the factory model. It can also be said to be an extension of the factory model. So what is the abstract factory model? Well, let's take a look at this question: Suppose we are playing a horizontal pass game. There are several types of monsters (such as Genie, gender, ghost…) In the current level ...), There are dozens of monsters in each type, so we should encapsulate and manage the scenario objects like the factory model. The difference between the abstract factory mode and the factory mode is that the abstract factory class manages a group of basic objects, while the factory class only manages a class of basic objects. Next let's take a look at the structure example diagram:
Conclusion: Both the factory model and the abstract factory model are encapsulated to manage object creation (new. The only difference between them is that one is to manage the creation of a class of objects, and the other is to manage the creation of a group of objects. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxoMiBpZD0 = "example"> example
Suppose there is such A level, including two types of monsters: Class A and Class B (A group of objects ). At the same time, this level has two difficulty options: elementary and intermediate (two factory classes ). The following is an example code for creating a level MONSTER:
Category A and category B monsters
# Ifndef _ DesignMode _ Product __# define _ DesignMode _ Product _ // class A monster base class AbstractProductA {public: virtual ~ AbstractProductA (); protected: AbstractProductA () ;}; // class AbstractProductB of class B monster base {public: virtual ~ AbstractProductB (); protected: AbstractProductB () ;}; // class ProductA1: public AbstractProductA {public: ProductA1 ();~ ProductA1 () ;}; // class ProductA2: public AbstractProductA {public: ProductA2 ();~ ProductA2 () ;}; // class ProductB1: public AbstractProductB {public: ProductB1 ();~ ProductB1 () ;}; // class ProductB2: public AbstractProductB {public: ProductB2 ();~ ProductB2 () ;};# endif
# Include Product. h # include
Using namespace std; AbstractProductA: AbstractProductA () {} AbstractProductA ::~ AbstractProductA () {} AbstractProductB: AbstractProductB () {} AbstractProductB ::~ AbstractProductB () {} ProductA1: ProductA1 () {cout <elementary class A monster... <endl;} ProductA1 ::~ ProductA1 () {} ProductA2: ProductA2 () {cout <intermediate A type monster... <endl;} ProductA2 ::~ ProductA2 () {} ProductB1: ProductB1 () {cout <Elementary B type monster... <endl;} ProductB1 ::~ ProductB1 () {} ProductB2: ProductB2 () {cout <intermediate B type monster... <endl;} ProductB2 ::~ ProductB2 (){}
Factory (base class): Create a preliminary monster factory and an intermediate monster Factory (derived class)
# Ifndef _ DesignMode _ Factory __# define _ DesignMode _ Factory _ class AbstractProductA; class AbstractProductB; // Abstract Factory base class AbstractFactory {public: virtual ~ AbstractFactory (); virtual AbstractProductA * CreateProductA () = 0; virtual AbstractProductB * CreateProductB () = 0; protected: AbstractFactory (); // The abstract class constructor is written as protected, prevent external constructor abstract class private :}; // create the elementary monster factory class ConcreteFactory1: public AbstractFactory {public: ConcreteFactory1 ();~ ConcreteFactory1 (); AbstractProductA * CreateProductA (); AbstractProductB * CreateProductB (); protected: private :}; // create an intermediate monster factory class ConcreteFactory2: public AbstractFactory {public: ConcreteFactory2 (); ~ ConcreteFactory2 (); AbstractProductA * CreateProductA (); AbstractProductB * CreateProductB (); protected: private :}; # endif
#include Factory.h#include Product.h#include
using namespace std;AbstractFactory::AbstractFactory(){}AbstractFactory::~AbstractFactory(){}ConcreteFactory1::ConcreteFactory1(){}ConcreteFactory1::~ConcreteFactory1(){}AbstractProductA* ConcreteFactory1::CreateProductA(){ return new ProductA1();}AbstractProductB* ConcreteFactory1::CreateProductB(){ return new ProductB1();}ConcreteFactory2::ConcreteFactory2(){}ConcreteFactory2::~ConcreteFactory2(){}AbstractProductA* ConcreteFactory2::CreateProductA(){ return new ProductA2();}AbstractProductB* ConcreteFactory2::CreateProductB(){ return new ProductB2();}
Main Test
# Include Factory. h # include
Using namespace std; int main (int argc, char * argv []) {// beginner monster AbstractFactory * cf1 = new ConcreteFactory1 (); cf1-> CreateProductA (); cf1-> CreateProductB (); // intermediate monster AbstractFactory * cf2 = new ConcreteFactory2 (); cf2-> CreateProductA (); cf2-> CreateProductB (); return 0 ;}