I have an ABC with several derived classes. To create these derived classes I use the factory pattern:
.h file:
class derivedFactory
{
public:
base* createInstance();
};
.cpp file:
base* derivedFactory::createInstance()
{
new derived();
}
Is there any advantage to this over just having a free function:
.h file:
base* derivedFactoryFunction();
.cpp file:
base* derivedFactoryFunction()
{
new derived();
}
Also: I use the abstract factory pattern for dependency injection. I might use an inheritance hierarchy based on the ABC:
class objectCreator
{
public:
base* create() = 0;
};
Is there any advantage to using this over a function pointer:
boost::function<base* ()> factory_ptr;
Using boost::bind/lambda this seems to make my code more composable, and if I wish I can wrap a real factory object in it. I can see that there may be a slight performance decrease but this is much to worry about as it is only called during startup.