What exactly is a Class Factory?

Here’s some supplemental information that may help better understand several of the other shorter, although technically correct, answers.

In the strictest sense a Class Factory is a function or method that creates or selects a class and returns it, based on some condition determined from input parameters or global context. This is required when the type of object needed can’t be determined until runtime. Implementation can be done directly when classes are themselves objects in the language being used, such as Python.

Since the primary use of any class is to create instances of itself, in languages such as C++ where classes are not objects that can be passed around and manipulated, a similar result can often be achieved by simulating “virtual constructors”, where you call a base-class constructor but get back an instance of some derived class. This must be simulated because constructors can’t really be virtual in C++, which is why such object—not class—factories are usually implemented as standalone functions or static methods.

Although using object-factories is a simple and straight-forward scheme, they require the manual maintenance of a list of all supported types in the base class’ make_object() function, which can be error-prone and labor-intensive (if not over-looked). It also violates encapsulation✶✶ since a member of base class must know about all of the base’s concrete descendant classes (now and in the future).

Virtual functions are normally resolved “late” by the actual type of object referenced, but in the case of constructors, the object doesn’t exist yet, so the type must be determined by some other means.
✶✶ Encapsulation is a property of the design of a set of classes and functions where the knowledge of the implementation details of a particular class or function are hidden within it—and is one of the hallmarks of object-oriented programming.

Therefore the best/ideal implementations are those that can handle new candidate classes automatically when they’re added, rather than having only a certain finite set currently hardcoded into the factory (although the trade-off is often deemed acceptable since the factory is the only place requiring modification).

James Coplien’s 1991 book Advanced C++: Programming Styles and Idioms has details on one way to implement such virtual generic constructors in C++. There are even better ways to do this using C++ templates, but that’s not covered in the book which predates their addition to the standard language definition. In fact, C++ templates are themselves class factories since they instantiate a new class whenever they’re invoked with different actual type arguments.
Update: I located a 1998 paper Coplien wrote for EuroPLoP titled C++ Idioms where, among other things, he revises and regroups the idioms in his book into design-pattern form à la the 1994 Design Patterns: Elements of Re-Usable Object-Oriented Software book. Note especially the Virtual Constructor section (which uses his Envelope/Letter pattern structure).

Also see the related answers here to the question Class factory in Python as well as the 2001 Dr. Dobb’s article about implementing them with C++ Templates titled Abstract Factory, Template Style.

Leave a Comment