首次 博客 记录自己的学习历程
有错误之处,还请提出
代码
#pragma once #include "string" #include "memory" #include "map" template<class ManufacturedType, typename ClassIDKey = std::string> class GenericFactory { typedef std::tr1::shared_ptr<ManufacturedType>(*BaseCreateFn)(); typedef std::map<ClassIDKey, BaseCreateFn> FnRegistry; FnRegistry registry; GenericFactory(){} GenericFactory(const GenericFactory&); GenericFactory& operator=(const GenericFactory&); public: static GenericFactory & instance() { static GenericFactory bf; return bf; } bool RegCreateFn( const ClassIDKey & className, BaseCreateFn Fn ) { registry[className] = Fn; return true; } std::tr1::shared_ptr<ManufacturedType> Create(const ClassIDKey & className )const { std::tr1::shared_ptr<ManufacturedType> theObject; FnRegistry::const_iterator regEntry = registry.find( className ); if ( regEntry != registry.end() ) { theObject = regEntry->second(); } return theObject; } }; template<class ManufacturedType, typename ClassIDKey = std::string> class RegisterInFactory { private: static std::tr1::shared_ptr<ManufacturedType> theObj; static bool m_bIsSig; public: bool& GetIsSig(){ return m_bIsSig; } static std::tr1::shared_ptr<ManufacturedType> CreateInstance() { if( (theObj.get() ==NULL && m_bIsSig == true) || m_bIsSig==false ) { theObj = std::tr1::shared_ptr<ManufacturedType>(new ManufacturedType); } else if (theObj.get() != NULL && m_bIsSig == true ) { } return theObj; } void Register( const ClassIDKey& id ) { GenericFactory<ManufacturedType>::instance().RegCreateFn(id,CreateInstance); } RegisterInFactory(const ClassIDKey& id, bool m_bSig = false) { m_bIsSig = m_bSig; GenericFactory<ManufacturedType>::instance().RegCreateFn(id,CreateInstance); } static std::tr1::shared_ptr<ManufacturedType> GetInstance( const ClassIDKey& id ) { return GenericFactory<ManufacturedType>::instance().Create( id ); } }; //注册 #define FACTORY_REGISTRY( classType ) \ template< >bool RegisterInFactory<classType>::m_bIsSig = false; \ template< >std::tr1::shared_ptr<classType> RegisterInFactory<classType>::theObj ; \ RegisterInFactory<classType> classType##123(std::string(#classType)) ; //注册单列,感觉多余了 #define FACTORY_REGISTRY_SINGLETON( classType ) \ template< >bool RegisterInFactory<classType>::m_bIsSig = true; \ template< >std::tr1::shared_ptr<classType> RegisterInFactory<classType>::theObj; \ RegisterInFactory<classType> classType##123(std::string(#classType),true) ; //转成单列的 #define FACTORY_CONTEXT_TO_SINGLETON( classType ) \ classType##123.GetIsSig() = true; //同上,反向 #define FACTORY_CONTEXT_TO_NORMAL( classType ) \ classType##123.GetIsSig() = false; //得到对象的指针 #define FACTORY_GET(classType) \ RegisterInFactory<classType>::GetInstance(std::string(#classType)).get() //专程对应的指针类型 #define FACTORY_GET_DYNAMIC(classType,classToType) \ (std::tr1::dynamic_pointer_cast<classToType>(RegisterInFactory<classType>::GetInstance(std::string(#classType)))).get()
使用:
#include "stdafx.h" #include "iostream" #include "conio.h" #include "GenericFactory.hpp" class Animal { public: virtual void PrintType() { std::cout<<"Animal"<<std::endl; } }; class Bird : public Animal { public: virtual void PrintType() { std::cout<<"Bird"<<std::endl; } }; FACTORY_REGISTRY( Animal ) FACTORY_REGISTRY_SINGLETON(Bird) int _tmain(int argc, _TCHAR* argv[]) { Animal *pAnimal = new Animal; Animal *pAnimal2 = FACTORY_GET(Animal); FACTORY_CONTEXT_TO_SINGLETON(Animal) Animal *pAnimal3 = FACTORY_GET(Animal); if ( pAnimal2 == pAnimal3 ) { std::cout<<"Same Point"<<std::endl; } Animal *pBird2Animal = FACTORY_GET_DYNAMIC( Bird,Animal ); pAnimal->PrintType(); pAnimal2->PrintType(); pBird2Animal->PrintType(); _getch(); return 0; }
输出:
Same Point
Animal
Animal
Bird刚开始接触设计模式,有错误,请提出下,多谢。
注:参考 <<C++对话系列>>