#ifndef ABSTRACTFACTORY_H #define ABSTRACTFACTORY_H class AbstractProductA; class AbstractProductB; class AbstractFactory { public: virtual ~AbstractFactory(); virtual AbstractProductA* CreateProductA() = 0; virtual AbstractProductB* CreateProductB() = 0; protected: AbstractFactory(); private: }; class ConcreteFactory1:public AbstractFactory { public: ConcreteFactory1(); ~ConcreteFactory1(); AbstractProductA* CreateProductA(); AbstractProductB* CreateProductB(); protected: private: }; class ConcreteFactory2:public AbstractFactory { public: ConcreteFactory2(); ~ConcreteFactory2(); AbstractProductA* CreateProductA(); AbstractProductB* CreateProductB(); protected: private: }; #endif // ABSTRACTFACTORY_H
#include "AbstractFactory.h" #include "Product.h" 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(); }
#ifndef PRODUCT_H #define PRODUCT_H class AbstractProductA { public: virtual ~AbstractProductA(); protected: AbstractProductA(); private: }; class AbstractProductB { public: virtual ~AbstractProductB(); protected: AbstractProductB(); private: }; class ProductA1:public AbstractProductA { public: ProductA1(); ~ProductA1(); protected: private: }; class ProductA2:public AbstractProductA { public: ProductA2(); ~ProductA2(); protected: private: }; class ProductB1:public AbstractProductB { public: ProductB1(); ~ProductB1(); protected: private: }; class ProductB2:public AbstractProductB { public: ProductB2(); ~ProductB2(); protected: private: }; #endif // PRODUCT_H
#include "Product.h" #include <iostream> using namespace std; AbstractProductA::AbstractProductA() { } AbstractProductA::~AbstractProductA() { } AbstractProductB::AbstractProductB() { } AbstractProductB::~AbstractProductB() { } ProductA1::ProductA1() { cout << "ProductA1..." << endl; } ProductA1::~ProductA1() { } ProductA2::ProductA2() { cout << "ProductA2..." << endl; } ProductA2::~ProductA2() { } ProductB1::ProductB1() { cout << "ProductB1..." << endl; } ProductB1::~ProductB1() { } ProductB2::ProductB2() { cout << "ProductB2..." << endl; } ProductB2::~ProductB2() { }
#include "AbstractFactory.h"
int main(int argc, char *argv[])
{
AbstractFactory* cf1 = new ConcreteFactory1;
cf1->CreateProductA();
cf1->CreateProductB();
AbstractFactory* cf2 = new ConcreteFactory2;
cf2->CreateProductA();
cf2->CreateProductB();
}
AbstractFactory模式是为创建一组(有多类)相关或依赖的对象提供创建接口,而Factory模式正如我在相应的文档中分析的是为一类对象提供创建接口或延迟对象的创建到子类中实现。并且可以看到,AbstractFactory模式通常都是使用Factory模式实现(ConcreteFactory1)。
它的模型如下:
如果你还看不懂这个模型的用意,那你可以想象一下,一些打怪的游戏,每关都有不同级别的怪物,所以你要为每个关卡生成不同级别的怪物,而如果仍采用Factory模型,因为只有一个工厂生成怪物,因此难以维护。这个模式可以为不同关卡生成不同类型的怪物,当维护的时候,只需要改变各个关卡的怪物类即可。