—————— C++版——————
template<class TBase,class TConcreate>
class CConcreteFactory
{
public:
static TBase* create()
{
reutrn new TConcreate();
}
}
template<calss TBase>
class CBaseFactory
{
private:
CBaseFactory(){}
~CBaseFactory(){}
CBaseFactory(const CBaseFactory& obj){}
CBaseFactory operator = (const CBaseFactory& obj){}
public:
static CBaseFactory* Instance()
{
static CBaseFactory<TBase> *pInstance;
if(pInstance == NULL)
{
pInstance = new CBaseFactory();
}
return pInstance;
}
private:
map<string,TBase*> mConcreateFactory;
public:
template<class TConcreate>
void registerFactory(const std::string& _type)
{
mConcreateFactory[_type] = CConcreateFactory<TBase,TConcreate>:create();
}
void destoryAllFactory()
{
mConcreateFactory.clear();
}
TBase* create(const std::sting& _type)
{
TBase* pBase = mConcreateFactory[_type];
}
void destory(const std:string& _type)
{
TBase* pBase = mConcreateFactory[_type];
if(pBase != 0)
delete pBase;
}
}