AbstactFactory模式
作者:互联网
AbstractFactory模式就是用来解决这类问题的:要创建一组相关或者相互依赖的对象。
AbstractFactory模式关键就是将这一组对象的创建封装到一个用于创建对象的类(ConcreteFactory)中,
维护这样一个创建类总比维护n多相关对象的创建过程要简单的多。
Product.h
1 #ifndef _PRODUCT_H_ 2 #define _PRODUCT_H_ 3 class AbstractProductA { 4 public: 5 virtual ~AbstractProductA(); 6 protected: 7 AbstractProductA(); 8 private: 9 }; 10 class AbstractProductB { 11 public: 12 virtual ~AbstractProductB(); 13 protected: 14 AbstractProductB(); 15 private: 16 }; 17 class ProductA1:public AbstractProductA { 18 public: 19 ProductA1(); 20 ~ProductA1(); 21 protected: 22 private: 23 }; 24 class ProductA2:public AbstractProductA { 25 public: 26 ProductA2(); 27 ~ProductA2(); 28 protected: 29 private: 30 }; 31 class ProductB1:public AbstractProductB { 32 public: 33 ProductB1(); 34 ~ProductB1(); 35 protected: 36 private: 37 }; 38 class ProductB2:public AbstractProductB { 39 public: 40 ProductB2(); 41 ~ProductB2(); 42 protected: 43 private: 44 }; 45 #endif //~_PRODUCT_H_
Product.cpp
1 #include "Product.h" 2 #include <iostream> 3 using namespace std; 4 AbstractProductA::AbstractProductA() { 5 } 6 AbstractProductA::~AbstractProductA() { 7 } 8 AbstractProductB::AbstractProductB() { 9 } 10 AbstractProductB::~AbstractProductB() { 11 } 12 ProductA1::ProductA1() { 13 cout<<"ProductA1..."<<endl; } 14 ProductA1::~ProductA1() { 15 } 16 ProductA2::ProductA2() { 17 cout<<"ProductA2..."<<endl; } 18 ProductA2::~ProductA2() { 19 } 20 ProductB1::ProductB1(){ 21 cout<<"ProductB1..."<<endl; } 22 ProductB1::~ProductB1() { 23 } 24 ProductB2::ProductB2() { 25 cout<<"ProductB2..."<<endl; } 26 ProductB2::~ProductB2() { 27 }
AbstractFactory.h
1 #ifndef _ABSTRACTFACTORY_H_ 2 #define _ABSTRACTFACTORY_H_ 3 4 class AbstractProductA; 5 class AbstractProductB; 6 7 class AbstractFactory { 8 public: 9 virtual ~AbstractFactory(); 10 virtual AbstractProductA* CreateProductA() = 0; 11 virtual AbstractProductB* CreateProductB() = 0; 12 protected: 13 AbstractFactory(); 14 private: 15 }; 16 class ConcreteFactory1:public AbstractFactory { 17 public: 18 ConcreteFactory1(); 19 ~ConcreteFactory1(); 20 AbstractProductA* CreateProductA(); 21 AbstractProductB* CreateProductB(); 22 protected: 23 private: 24 }; 25 class ConcreteFactory2:public AbstractFactory { 26 public: 27 ConcreteFactory2(); 28 ~ConcreteFactory2(); 29 AbstractProductA* CreateProductA(); 30 AbstractProductB* CreateProductB(); 31 protected: 32 private: 33 }; 34 #endif //~_ABSTRACTFACTORY_H_
AbstractFactory.cpp
1 #include "AbstractFactory.h" 2 #include "Product.h" 3 #include <iostream> 4 using namespace std; 5 AbstractFactory::AbstractFactory() { 6 } 7 AbstractFactory::~AbstractFactory() 8 { 9 } 10 ConcreteFactory1::ConcreteFactory1() { 11 } 12 ConcreteFactory1::~ConcreteFactory1() { 13 } 14 AbstractProductA* ConcreteFactory1::CreateProductA() { return new ProductA1(); } 15 AbstractProductB* ConcreteFactory1::CreateProductB() { return new ProductB1(); } 16 ConcreteFactory2::ConcreteFactory2() { 17 } 18 ConcreteFactory2::~ConcreteFactory2() { 19 } 20 AbstractProductA* ConcreteFactory2::CreateProductA() { return new ProductA2(); } 21 AbstractProductB* ConcreteFactory2::CreateProductB() { return new ProductB2(); }
main.cpp
1 #include "AbstractFactory.h" 2 #include <iostream> 3 using namespace std; 4 int main(int argc,char* argv[]) { 5 AbstractFactory* cf1 = new ConcreteFactory1(); 6 cf1->CreateProductA(); 7 cf1->CreateProductB(); 8 AbstractFactory* cf2 = new ConcreteFactory2(); 9 cf2->CreateProductA(); 10 cf2->CreateProductB(); 11 return 0; 12 }
AbstractFactory模式和Factory模式的区别是初学(使用)设计模式时候的一个容易引起困惑的地方。实际上,AbstractFactory模式是为创建一组(有多类)相关或依赖的对象提供创建接口,而Factory模式正如我在相应的文档中分析的是为一类对象提供创建接口或延迟对象的创建到子类中实现。并且可以看到,AbstractFactory模式通常都是使用Factory模式实现(ConcreteFactory1)。
标签:ConcreteFactory1,class,AbstractProductA,模式,AbstractProductB,AbstractFactory,Abst 来源: https://www.cnblogs.com/Malphite/p/10984817.html