Although the orthodox Factory pattern uses string comparisons to determine the actual derived object, there is a cleaner and more efficient way to achieve the same results. Replacing the string argument of create() with an enumeration will produce significantly faster code and enable you to use a switch which is more readable and easier to maintain than a list of else-ifstatements.
First, add an enumeration into the Image Factory class:
//...add new image formats as necessary
Next, add a switch block to create():
static Image* create(ImgFormat fmt)
return new JpegImage;
return new BmpImage;
return new PngImage;
Finally, change main()accordingly:
auto_ptr<const Image> img(factory.create(ImgFactory::JPG));
A Timeless Solution
Recently, I've seen several questions in the C++ forumregarding techniques for creating derived objects at runtime. In all these cases, Factory has proved to be the right solution. This only goes to show that classic design patterns are still as relevant as ever.