标签:os io ar 代码 amp sp new on c
抽象工厂模式和工厂模式差不多,在工厂模式的基础上相当于给产品增加了型号。两种产品A和B,每种产品有两个型号1和2,两个工厂1和2分别用来生产类型1和2的产品
代码如下:
#include <iostream> using namespace std; class ProductA { public: ProductA(){} ~ProductA(){} private: ProductA(const ProductA& product){} ProductA& operator = (const ProductA& product){} public: virtual void DoSomeThingA() = 0; }; class ProductA1 : public ProductA { public: ProductA1(){} ~ProductA1(){} private: ProductA1(const ProductA1& product){} ProductA1& operator = (const ProductA1& product){} public: void DoSomeThingA() { cout<< "Produce ProduceA1"<< endl; } }; class ProductA2 : public ProductA { public: ProductA2(){} ~ProductA2(){} private: ProductA2(const ProductA2& product){} ProductA2& operator = (const ProductA2& product){} public: void DoSomeThingA() { cout<< "Produce ProduceA2"<< endl; } }; class ProductB { public: ProductB(){} ~ProductB(){} private: ProductB(const ProductB& product){} ProductB& operator = (const ProductB& product){} public: virtual void DoSomeThingB() = 0; }; class ProductB1 : public ProductB { public: ProductB1(){} ~ProductB1(){} private: ProductB1(const ProductB1& product){} ProductB1& operator = (const ProductB1& product){} public: void DoSomeThingB() { cout<< "Produce ProduceB1"<< endl; } }; class ProductB2 : public ProductB { public: ProductB2(){} ~ProductB2(){} private: ProductB2(const ProductB2& product){} ProductB2& operator = (const ProductB2& product){} public: void DoSomeThingB() { cout<< "Produce ProduceB2"<< endl; } }; class AbstractFactory { public: AbstractFactory() {} ~AbstractFactory(){} private: AbstractFactory(const AbstractFactory& A){} AbstractFactory& operator = (const AbstractFactory& A){} public: virtual ProductA * CreateProductA() = 0; virtual ProductB * CreateProductB() = 0; }; class Factory1 : public AbstractFactory { public: Factory1() {} ~Factory1(){} private: Factory1(const Factory1& f1){} Factory1& operator = (const Factory1& f1){} public: ProductA * CreateProductA() { ProductA* tmp = new ProductA1(); return tmp; } ProductB * CreateProductB() { ProductB* tmp = new ProductB1(); return tmp; } }; class Factory2 : public AbstractFactory { public: Factory2() {} ~Factory2(){} private: Factory2(const Factory2& f2){} Factory2& operator = (const Factory2& f2){} public: ProductA * CreateProductA() { ProductA* tmp = new ProductA2(); return tmp; } ProductB * CreateProductB() { ProductB* tmp = new ProductB2(); return tmp; } };
#include "main.h" using namespace std; int main(int argc, char* argv[]) { AbstractFactory* factory1 = new Factory1(); AbstractFactory* factory2 = new Factory2(); ProductA* productA1 = factory1->CreateProductA(); productA1->DoSomeThingA(); ProductB* productB1 = factory1->CreateProductB(); productB1->DoSomeThingB(); ProductA* productA2 = factory2->CreateProductA(); productA2->DoSomeThingA(); ProductB* productB2 = factory2->CreateProductB(); productB2->DoSomeThingB(); }
标签:os io ar 代码 amp sp new on c
原文地址:http://my.oschina.net/hejiula/blog/306495