/********************************* *设计模式--抽象工厂实现 *C++语言 *Author:WangYong *Blog:http://www.cnblogs.com/newwy ********************************/ #include <iostream> using namespace std; class AbstractProductA { public: virtual ~AbstractProductA() = 0; AbstractProductA(){}; }; AbstractProductA::~AbstractProductA(){} class ProductA1:public AbstractProductA { public: ProductA1(){cout<<"ProductA1"<<endl;} }; class ProductA2:public AbstractProductA { public: ProductA2(){cout<<"ProductA2"<<endl;} }; class AbstractProductB { public: virtual ~AbstractProductB() = 0; AbstractProductB(){}; }; AbstractProductB::~AbstractProductB(){} class ProductB1:public AbstractProductB { public: ProductB1(){cout<<"ProductB1"<<endl;} }; class ProductB2:public AbstractProductB { public: ProductB2(){cout<<"ProductB2"<<endl;} }; class AbstractFactory { public: virtual ~AbstractFactory(); virtual AbstractProductA *CreateProductA() = 0; virtual AbstractProductB *CreateProductB() = 0; }; AbstractFactory::~AbstractFactory(){} class ConcreteFactory1:public AbstractFactory { public: ConcreteFactory1(){cout<<"ConcreteFactory1..."<<endl;} AbstractProductA *CreateProductA() {return new ProductA1();} AbstractProductB *CreateProductB() {return new ProductB1();} }; class ConcreteFactory2:public AbstractFactory { public: ConcreteFactory2(){cout<<"ConcreteFactory2..."<<endl;} AbstractProductA *CreateProductA(){return new ProductA2();} AbstractProductB *CreateProductB(){return new ProductB2();} }; int main() { AbstractFactory *cf1 = new ConcreteFactory1(); cf1->CreateProductA();cf1->CreateProductB(); AbstractFactory *cf2 = new ConcreteFactory2(); cf2->CreateProductA();cf2->CreateProductB(); return 0; }