In Object Oriented Programming we create systems that manage objects. However, it is always a good idea to separate the implementation of the objects from the systems. This way, we can add or change objects and the system doesn’t need to change.
This post is related to object Factories. A factory is responsible for creating objects. Here is a very simple example of an object factory:
IObject * ObjectFactory::CreateObject(const string & objectType)
{
if (objectType == “Model”)
return new Model();
else if (objectType == “Sound”)
return new Sound();
else if (objectType == “Light”)
return new Light();
return NULL;
}
Each of these 3 objects derive from the IObject interface. Based on the type of the object (”Model”, “Sound”, or “Light”) we create that object (new Model(), etc).
This is great. But we can improve…
What if we want to get a list of object types that the factory is able to create? This was my question and here is the way I solved the problem.
First we create an interface for our object factory. This would allow us to create multiple object factories for different parts of an application (one for the map editor, one for the driving level, etc) or for different applications all together.
class IObjectFactory
{
public:
IObject *(*Creator)(void);
virtual ~IObjectFactory() = 0 { }
/**
* Returns a list of objects that this factory can create
**/
list<string> GetCreationTypes(void);
/**
* Create an object of the type passed in
**/
IObject * CreateObject(const string & objectType);
protected:
void RegisterCreator(const std::string & objectType, IObjectFactory::Creator creator);
void UnregisterCreator(const std::string & objectType);
private:
IObjectFactory::Creator * GetCreator(const std::string & objectType);
map<string, IObjectFactory::Creator> objectCreators;
};
The concept is that we can Register a Creator into the IObjectFactory and then the factory will handle everything for us. We can create an object or get a list of object types that the factory can create at runtime without any additional modifications from us. The implementation for this interface is trivial…
This line is important:
typedef IObject *(*Creator)(void);
This defines a function pointer to a “Creator” function. This is a function that will create a specific object for us.
Here is a templated “Creator” for you to use:
template <typename T>
IObject * ObjectCreateFunction(void)
{
return new T();
}
Then simply inherit from IObjectFactory in your concrete object factory and define a constructor similar to this:
ConcreateObjectFactory::ConcreateObjectFactory()
{
RegisterCreator(”Model”, &ObjectCreateFunction<Model>);
RegisterCreator(”Sound”, &ObjectCreateFunction<Sound>);
RegisterCreator(”Light”, &ObjectCreateFunction<Light>);
}
P.S. I know that the IObjectFactory isn’t a purely an interface but I am still going to call it an interface. Deal with it.
















