首页 > 代码库 > Abstract_Factory
Abstract_Factory
#include <iostream>using namespace std;#define DESTORY_POINTER(ptr) if (ptr) { delete ptr; ptr = NULL; }class ProductFamilyA{public: virtual ~ProductFamilyA() {} virtual void Action()=0; protected: ProductFamilyA() {}};class ProductFamilyB{public: virtual ~ProductFamilyB() {} virtual void Action()=0; protected: ProductFamilyB() {}};class Factory{public: virtual ~Factory() {} virtual ProductFamilyA* CreateProductA()=0; virtual ProductFamilyB* CreateProductB()=0;protected: Factory() {}};class ProductA1 : public ProductFamilyA{public: ProductA1() {} void Action() { cout<<"ProductFamilyA->ProductA1::Action"<<endl; }};class ProductA2 : public ProductFamilyA{public: ProductA2() {} void Action() { cout<<"ProductFamilyA->ProductA2::Action"<<endl; }};class ProductB1 : public ProductFamilyB{public: ProductB1() {} void Action() { cout<<"ProductFamilyB->ProductB1::Action"<<endl; }};class ProductB2 : public ProductFamilyB{public: ProductB2() {} void Action() { cout<<"ProductFamilyB->ProductB2::Action"<<endl; }};class Factory1 : public Factory{public: ProductFamilyA* CreateProductA() { return new ProductA1; } ProductFamilyB* CreateProductB() { return new ProductB1; }};class Factory2 : public Factory{public: ProductFamilyA* CreateProductA() { return new ProductA2; } ProductFamilyB* CreateProductB() { return new ProductB2; }};int main(int argc, char *argv[]){ Factory* pFactory = NULL; ProductFamilyA* pProductFamilyA = NULL; ProductFamilyB* pProductFamilyB = NULL; pFactory = new Factory1; pProductFamilyA = pFactory->CreateProductA(); pProductFamilyB = pFactory->CreateProductB(); pProductFamilyA->Action(); pProductFamilyB->Action(); DESTORY_POINTER(pFactory); DESTORY_POINTER(pProductFamilyA); DESTORY_POINTER(pProductFamilyB); pFactory = new Factory2; pProductFamilyA = pFactory->CreateProductA(); pProductFamilyB = pFactory->CreateProductB(); pProductFamilyA->Action(); pProductFamilyB->Action(); DESTORY_POINTER(pFactory); DESTORY_POINTER(pProductFamilyA); DESTORY_POINTER(pProductFamilyB); return 0;}
Abstract_Factory
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。