国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > 综合技术 > Factory Pattern(工厂模式)

Factory Pattern(工厂模式)

来源:程序员人生   发布时间:2015-06-06 08:06:45 阅读次数:2313次

Factory Pattern(C++实现)

/*Product.h*/ #ifndef PRODUCT_H #define PRODUCT_H class Product { public: virtual ~Product()=0; protected: Product(); private: }; class ConcreteProduct:public Product { public: ~ConcreteProduct(); ConcreteProduct(); protected: private: }; #endif

/*Product.cpp*/ #include "Product.h" #include <iostream> Product::Product() { } Product::~Product() { } ConcreteProduct::ConcreteProduct() { std::cout<<"ConcreteProduct..."<<std::endl; } ConcreteProduct::~ConcreteProduct() { }

/*Factory.h*/ #ifndef FACTORY_H #define FACTORY_H class Product; class Factory { public: virtual ~Factory()=0; virtual Product *CreateProduct()=0; protected: Factory(); private: }; class ConcreteFactory:public Factory { public; ~ConcreteFactory(); ConcreteFactory(); Product *CreateProduct(); protected: private: }; #endif

/*Factory.cpp*/ #include "Factory.h" #include "Product.h" #include <iostream> Factory::Factory() { } Factory::~Factory() { } ConcreteFactory::ConcreteFactory() { std::cout<<"ConcreteFactory..."<<std::endl; } ConcreteFactory::~ConcreteFactory() { } Product *ConcreteFactory::CreateProduct() { return new ConcreteProduct(); }
/*main.cpp*/ #include "Factory.h" #include "Product.h" #include <iostream> int main() { Factory *fac=new ConcreteFactory(); Product *p=fac->CreateProduct(); return 0; }



生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生