Browse DevX
Sign up for e-mail newsletters from DevX


Use the Factory Pattern to Facilitate Dynamic Typing-3 : Page 3

An abstract base class from which concrete classes are derived is a very common idiom in object-oriented code. So common indeed that a special design pattern called Factory was devised to simplify the runtime creation of derived objects. Learn how Factory can boost your code's reliability and performance.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Factory Refactored

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:

class ImageFactory { public: enum ImgFormat { JPG, BMP, //...add new image formats as necessary DEFAULT }; };

Next, add a switch block to create():

static Image* create(ImgFormat fmt) { switch (fmt) { case JPG: return new JpegImage; break; case BMP: return new BmpImage; break; default: return new PngImage; } }

Finally, change main()accordingly:

int main() { ImageFactory factory; auto_ptr<const Image> img(factory.create(ImgFactory::JPG)); img->image_type(); img.reset(factory.create(ImageFactory::BMP)); img->image_type(); img.reset(factory.create(ImageFactory::DEFAULT)); img->image_type(); }

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.

Danny Kalev is a certified system analyst and software engineer specializing in C++. He was a member of the C++ standards committee between 1997 and 2000 and has since been involved informally in the C++0x standardization process. He is the author of "The ANSI/ISO Professional C++ Programmer's Handbook" and "The Informit C++ Reference Guide: Techniques, Insight, and Practical Advice on C++."
Thanks for your registration, follow us on our social networks to keep up-to-date