标签:os io ar amp new sp on c ios
工厂模式和简单工厂模式差不多,只不过将每一个分支判断变成了一个子工厂类
#include <iostream> using namespace std; class Product { public: Product(){} ~Product(){} private: Product(const Product& product){} Product& operator = (const Product& product){} public: virtual void DoSomeThing() = 0; }; class ProductA : public Product { public: ProductA(){} ~ProductA(){} private: ProductA(const ProductA& product){} ProductA& operator = (const ProductA& product){} public: void DoSomeThing() { cout<< "Produce ProduceA"<< endl; } }; class ProductB : public Product { public: ProductB(){} ~ProductB(){} private: ProductB(const ProductB& product){} ProductB& operator = (const ProductB& product){} public: void DoSomeThing() { cout<< "Produce ProduceB"<< endl; } }; class ProductC : public Product { public: ProductC(){} ~ProductC(){} private: ProductC(const ProductC& product){} ProductC& operator = (const ProductC& product){} public: void DoSomeThing() { cout<< "Produce ProduceC"<< endl; } }; class AbstractFactory { public: AbstractFactory() {} ~AbstractFactory(){} private: AbstractFactory(const AbstractFactory& A){} AbstractFactory& operator = (const AbstractFactory& A){} public: virtual Product * CreateProduct() = 0; }; class FactoryA : public AbstractFactory { public: FactoryA() {} ~FactoryA(){} private: FactoryA(const FactoryA& A){} FactoryA& operator = (const FactoryA& A){} public: Product * CreateProduct() { Product* tmp = new ProductA(); return tmp; } }; class FactoryB : public AbstractFactory { public: FactoryB() {} ~FactoryB(){} private: FactoryB(const FactoryB& B){} FactoryB& operator = (const FactoryB& B){} public: Product * CreateProduct() { Product* tmp = new ProductB(); return tmp; } }; class FactoryC : public AbstractFactory { public: FactoryC() {} ~FactoryC(){} private: FactoryC(const FactoryC& C){} FactoryC& operator = (const FactoryC& C){} public: Product * CreateProduct() { Product* tmp = new ProductC(); return tmp; } }; #include "main.h" using namespace std; int main(int argc, char* argv[]) { AbstractFactory* factory1 = new FactoryA(); AbstractFactory* factory2 = new FactoryB(); AbstractFactory* factory3 = new FactoryB(); Product* product1 = factory1->CreateProduct(); product1->DoSomeThing(); Product* product2 = factory2->CreateProduct(); product2->DoSomeThing(); Product* product3 = factory3->CreateProduct(); product3->DoSomeThing(); }
标签:os io ar amp new sp on c ios
原文地址:http://my.oschina.net/hejiula/blog/305522