/******************************************************************** file name : Factory.h author : Clark/陈泽丹 created : 2012-5-8 *********************************************************************/ #pragma #include <map> #include <algorithm> using namespace std; //映射工厂 template < typename _IdType, class _Fn > class Factory { public: bool Register(const _IdType& _ID, const _Fn& _Func) { return m_Map.insert(IdToFunMap::value_type(_ID, _Func)).second; } bool Unregister(const _IdType& _ID) { return m_Map.erase(_ID) == 1; } protected: Factory(){} typedef map<_IdType, _Fn> IdToFunMap; IdToFunMap m_Map; }; //工厂错误处理----------------------------------------------------------- template<typename _IdType, typename _ResultType> class FactoryError { protected: _ResultType OnUnknownType(const _IdType& _ID) { cout<<"Unknow object type pass to factory!"<<endl; return 0; } }; //辅助函数----------------------------------------------------------- template< class _FunClass> class MapFun { public: typedef typename _FunClass::result_type result_type; MapFun(_FunClass* _pFunClass):m_pFunClass(NULL) { m_pFunClass = _pFunClass->clone(); } MapFun(const MapFun& _other) { m_pFunClass = _other.m_pFunClass->clone(); } ~MapFun(){ if(NULL != m_pFunClass) delete m_pFunClass; m_pFunClass = NULL; } virtual result_type operator()(){ return (*m_pFunClass)(); } protected: _FunClass* m_pFunClass; }; //建造者工厂----------------------------------------------------------- template<class _ResultType> class BaseCreater { public: typedef _ResultType result_type; virtual result_type operator()() = 0; virtual BaseCreater<_ResultType>* clone() = 0; //{ return new BaseCreater<_ResultType>(*this);} }; template < typename _IdType, class _FunClass = BaseCreater<int> , template<typename, typename> class _FactoryErrorPolicy = FactoryError > class CreaterFactory: public Factory<_IdType, MapFun<_FunClass> >, _FactoryErrorPolicy<_IdType, typename MapFun<_FunClass> ::result_type> { public: typename MapFun<_FunClass> ::result_type CreateObject(const _IdType& _ID) { typename IdToFunMap::iterator it = m_Map.find(_ID); if( m_Map.end() != it) { return (it->second)(); } return OnUnknownType(_ID); } }; //辅助函数----------------------------------------------------------- template< class _FunClass> class MapFun2 { public: typedef typename _FunClass::result_type result_type; MapFun2(_FunClass* _pFunClass):m_pFunClass(NULL) { m_pFunClass = _pFunClass->clone(); } MapFun2(const MapFun2& _other) { m_pFunClass = _other.m_pFunClass->clone(); } ~MapFun2(){ if(NULL != m_pFunClass) delete m_pFunClass; m_pFunClass = NULL; } template<class _L, class _R> result_type operator()(_L* _pL, _R* pR){ return (*m_pFunClass)(_pL, pR); } protected: _FunClass* m_pFunClass; }; //封装反射类其工厂----------------------------------------------------------- class Reflex { private: enum {REAL_CLASS_ID = 0}; public: virtual int GetRealClassID() = 0; }; template<int v> class ReflexEx:public Reflex { protected: ReflexEx(){} virtual ~ReflexEx(){} public: enum {REAL_CLASS_EX_ID = v}; virtual int GetRealClassID(){ return REAL_CLASS_EX_ID; } }; //0不能用,因已做为基类ID了 template<> class ReflexEx<0>{}; //-------------------------------------------------- template<class _ResultType> class TypeHit { public: typedef _ResultType result_type; virtual result_type operator()(Reflex*, Reflex*) = 0; virtual TypeHit<_ResultType>* clone() = 0; //{ return new BaseCreater<_ResultType>(*this);} }; template < typename _IdType, class _FunClass = TypeHit<int> , template<typename, typename> class _FactoryErrorPolicy = FactoryError > class TypeHitFactory: protected Factory<std::pair<_IdType, _IdType>, MapFun2<_FunClass> >, _FactoryErrorPolicy<std::pair<_IdType, _IdType>, typename MapFun2<_FunClass> ::result_type> { public: typedef std::pair<_IdType, _IdType> KeyType; bool Register(const _IdType& lhsID, const _IdType& rhsID, const MapFun2<_FunClass>& _Func) { return Factory<std::pair<_IdType, _IdType>, MapFun2<_FunClass> >::Register(KeyType(lhsID, rhsID), _Func); } bool Unregister(const _IdType& lhsID, const _IdType& rhsID) { return Factory<std::pair<_IdType, _IdType>, MapFun2<_FunClass> >::Unregister(KeyType(lhsID, rhsID)); } typename MapFun2<_FunClass> ::result_type TypeHit(Reflex* _pL, Reflex* _pR) { typename IdToFunMap::iterator it = m_Map.find(KeyType(_pL->GetRealClassID(), _pR->GetRealClassID())); if( m_Map.end() != it) { return (it->second)(_pL, _pR); } return OnUnknownType(KeyType(_pL->GetRealClassID(), _pR->GetRealClassID())); } };
#include <vld.h> #include <iostream> #include "Factory.h" class A: public BaseCreater<int> { public: virtual result_type operator()(){ return 5;} virtual BaseCreater<int>* clone(){ return new A(*this);} }; class B: public BaseCreater<int> { public: virtual result_type operator()(){ return 7;} virtual BaseCreater<int>* clone(){ return new B(*this);} }; class LEx1: public ReflexEx<1> { public: virtual void Show(){ cout<<"LEx1"<<endl; } }; class REx1: public ReflexEx<2> { public: virtual void Show(){ cout<<"REx1"<<endl; } }; class LEx2: public ReflexEx<3> { public: virtual void Show(){ cout<<"LEx2"<<endl; } }; class REx2: public ReflexEx<4> { public: virtual void Show(){ cout<<"REx2"<<endl; } }; class Test: TypeHit<int> { public: virtual result_type operator()(Reflex* _lEx, Reflex* _rEx) { cout<<"("<<_lEx->GetRealClassID()<<" , "<<_rEx->GetRealClassID()<<") 注册了功能!"<<endl; return 0; } virtual TypeHit<int>* clone(){ return new Test(*this);} }; void main() { CreaterFactory<int, BaseCreater<int> > createrFactory; createrFactory.Register(1, MapFun< BaseCreater<int> >(&B())); cout<<createrFactory.CreateObject(1)<<endl; TypeHitFactory<int, TypeHit<int> > hitFactory; hitFactory.Register(LEx1::REAL_CLASS_EX_ID, REx1::REAL_CLASS_EX_ID, MapFun2< TypeHit<int> >(&Test())); Reflex* pL1 = new LEx1(); Reflex* pR1 = new REx1(); Reflex* pL2 = new LEx2(); Reflex* pR2 = new REx2(); hitFactory.TypeHit(pL1, pR1); hitFactory.TypeHit(pL2, pR2); delete pL1; delete pR1; delete pL2; delete pR2; system("pause"); }