首页 > 代码库 > 抽象工厂模式
抽象工厂模式
抽象工厂模式和工厂模式差不多,在工厂模式的基础上相当于给产品增加了型号。两种产品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(); }
抽象工厂模式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。