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() { return new derived(); }
Are there any advantages to this just having a free function:
.h file:
base* derivedFactoryFunction();
.cpp file:
base* derivedFactoryFunction() { return new derived(); }
Also: I use the abstract factory pattern to inject dependencies. I could use the ABC based inheritance hierarchy:
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 seems to make my code more complex, and if I want, I can wrap a real factory object in it. I see that there may be a slight decrease in performance, but there is a lot to worry about, since it is called only during startup.
c ++ design-patterns factory
Will manley
source share