one template factory
来源:互联网 发布:淘宝坑店知乎 编辑:程序博客网 时间:2024/05/30 23:51
#ifndef ProductFactory_H_#define ProductFactory_H_#include <map>template <typename Key, typename Product>struct creator_traits{typedef Product*PlainPointer;typedef Product*(*ProductCreator)();typedef std::map<Key, ProductCreator>ProductCreatorCollection;typedef void(*ProductDestroyor)(Product*);typedef std::map<Key, ProductDestroyor>ProductDestroyorCollection;};template<typename Key,typename Product,typename PlainPointer = typename creator_traits<Key, Product>::PlainPointer,typename ProductCreator = typename creator_traits<Key, Product>::ProductCreator,typename ProductCreatorCollection = typename creator_traits<Key, Product>::ProductCreatorCollection,typename ProductDestroyor = typename creator_traits<Key, Product>::ProductDestroyor,typename ProductDestroyorCollection = typename creator_traits<Key, Product>::ProductDestroyorCollection,>class ProductFactoryT{public:typedef Product ProductType;typedef ProductCreator ProductPtrCreator;typedef PlainPointer ProductPtr;typedef ProductDestroyor ProductPtrDestroyor;protected:ProductCreatorCollectionmyCreators;ProductDestroyorCollectionmyDestroyors;public:virtual bool addCreator(const Key &key, Creator creator){return myCreators.insert(std::make_pair<Key, Creator>(key, creator)).second;}virtual void removeCreator(const Key& key){myCreators.erase(key);}virtual PlainPointer create(const Key& key){if (myCreators.count(key)){return myCreators[key]();}}virtual bool exist(const Key& key){if (myCreators.find(key) != myCreators.end()){return true;}return false;}virtual bool addDestoryor(const Key&key, Destroyor destroyor){if (myDestroyors.find(key) == myDestroyors.end()){myDestroyors.insert(std::make_pair<Key, Creator>(key, creator));}return true;}virtual bool destroy(const Key&key, ProductPtr productPrt){if (myDestroyors.count(key)){myDestroyors[key](productPrt);return true;}else{return false;}}virtual void clear(){myCreators.clear();myDestroyors.clear();}};class MyClass{public:MyClass();~MyClass();private:};typedef ProductFactoryT<std::string,MyClass,creator_traits<std::string, MyClass>::PlainPointer,creator_traits<std::string, MyClass>::ProductCreator,creator_traits<std::string, MyClass>::ProductCreatorCollection> myFactory;myFactory myFactoryInstanse;//load myFactory::ProductPtrCreator myClassCreatorPtr = (myFactory::ProductPtrCreator)Library->getSymbol("create");myFactoryInstanse.addCreator("createMyClassGUID", myClassCreatorPtr);MyClass myc = myFactoryInstanse.create(createMyClassGUID);#endif // !ProductFactory_H_
0 0
- one template factory
- 对话#12:Abstract Factory, Template Style
- C++ Design Patterns : Template + Factory Method
- 对Factory,strategy,template模式的总结
- GWT (one template, multiple owner class/ one owner class, multiple template)
- Factory method pattern(air: different from the abstract one)
- template
- Template
- template
- template
- template
- Template
- template
- template
- Template
- template
- Template
- template
- STM32系统时钟硬件仿真查看
- 移动APP开发
- 华为机试题002
- 第1次实验——NPC问题(回溯算法、聚类分析)
- hdu 3938 Portal(离线并查集)
- one template factory
- C++ 静态成员变量和静态成员函数的练习
- Netty系列之Netty高性能之道
- 百度LBS面试
- <C/C++算法> 八大经典排序算法的性能对比与总结
- cxx 哎姆滴五工具类
- 贪心——Power of Cryptography
- memcpy的用法(与strcpy的区别)
- DBSCAN算法的C++实现