#pragma once #include #include #include template class AbstractCreator { public: virtual ~AbstractCreator() {} virtual Base* create(const IdType &id) const = 0; }; template class Creator : public AbstractCreator { public: Creator(IdType id) {} virtual ~Creator() {} Base* create(const IdType &id) const { return dynamic_cast(new ObjectClass(id)); } }; template class ObjectFactory { protected: typedef AbstractCreator AbstractFactory; typedef std::map FactoryMap; FactoryMap factory_; public: ObjectFactory() {} virtual ~ObjectFactory() { for (auto& item : factory_) delete item.second; } template void add(const IdType &id) { registerClass(id, new Creator(id)); } template Base* create(const IdType &idClass, const idCreator &id) { return factory_[idClass]->create(id); } protected: void registerClass(const IdType &id, AbstractFactory *ConcreteFabric) { typename FactoryMap::iterator it = factory_.find(id); if (it == factory_.end()) factory_[id] = ConcreteFabric; else delete ConcreteFabric; } };