#ifndef FACTORY_H #define FACTORY_H /////////////////////////////////////////////////////////////////////// // factory.h - declares object factory // // ver 2.0 // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2004 // /////////////////////////////////////////////////////////////////////// /* The objFactory class creates an object of the base or any derived class, attached to a base pointer. The user does not need to know the object's type. You need to treat the factory like a smart new operator. It creates an object on the heap. It's up to you to delete it. objFactory class declaration - The odd type T*(*)(), in the second argument of the map is the type of a function pointer for creation functions of signature T* somefun(). - In the reg function, the second declaration is Type arg where Type is T*(*)() and arg is pFun. */ #include #include typedef std::string classID; template class objFactory { public: objFactory(); void reg(classID id, Base*(*pFun)()); Base* makeObj(classID id); private: static std::map registry; }; // ///////////////////////////////////////////////////////////////////////// // define the static instance of the class registry template std::map objFactory::registry; //----< construct objFactory >------------------------------------------- template objFactory::objFactory() { } //----< register a static member creation function >--------------------- template void objFactory::reg(classID id, Base*(*pFun)()) { registry[id] = pFun; } //----< create an instance of a creatable class >------------------------ // // returns null pointer if object not found in registry // template Base* objFactory::makeObj(classID id) { Base *theObject = NULL; std::map::const_iterator entry = registry.find(id); if(entry != registry.end()) theObject = entry->second(); // using static creational function else theObject = 0; return theObject; } #endif