Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Heres's a little additional information that may help better understand some of the other technically correct, but shorter answers.</p> <p>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 <em>type</em> 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.</p> <p>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 <a href="https://en.wikipedia.org/wiki/Virtual_function" rel="nofollow noreferrer">virtual</a><sup><strong>✶</strong></sup> in C++, which is why such object—not class—factories are usually implemented as standalone functions or static methods.</p> <p><sup><strong>✶</strong> Virtual functions are normally resolved "<a href="https://en.wikipedia.org/wiki/Late_binding" rel="nofollow noreferrer">late</a>" 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.</sup></p> <p>The best implementations are those that handle new candidate classes automatically when they are added rather than having only a certain finite set currently hardcoded into the factory (although the trade-off is often acceptable if the factory is the only place requiring modification).</p> <p>James Coplien's 1991 book <a href="https://rads.stackoverflow.com/amzn/click/0201548550" rel="nofollow noreferrer"><strong><em>Advanced C++: Programming Styles and Idioms</em></strong></a> has details on one way to implement such virtual generic constructors in C++. There are even better ways to do this using <a href="http://web.archive.org/web/20010216204456/http://www.kirkrader.com/writing/technical/cpp.htm" rel="nofollow noreferrer">C++ templates</a>, but that was not covered in the book which predates their being added to the standard language definition. In fact, C++ templates themselves are class factories since they instantiate a new class whenever they're used with different actual type argument(s). <strong>Update:</strong> I located a 1998 paper he wrote for EuroPLoP titled <a href="http://web.archive.org/web/20080807045811im_/http://users.rcn.com:80/jcoplien/Patterns/C++Idioms/EuroPLoP98.html" rel="nofollow noreferrer"><strong><em>C++ Idioms</em></strong></a> where, among other things, he revises and regroups the idioms in his book into design-pattern form à la the 1994 <a href="https://rads.stackoverflow.com/amzn/click/0201633612" rel="nofollow noreferrer"><strong>Design Patterns: Elements of Re-Usable Object-Oriented Software</strong></a> book. Note especially the <a href="http://web.archive.org/web/20080807045811im_/http://users.rcn.com:80/jcoplien/Patterns/C++Idioms/EuroPLoP98.html#VirtualConstructor" rel="nofollow noreferrer">Virtual Constructor</a> section (which uses his <a href="http://web.archive.org/web/20080807045811im_/http://users.rcn.com:80/jcoplien/Patterns/C++Idioms/EuroPLoP98.html#EnvelopeLetter" rel="nofollow noreferrer">Envelope/Letter</a> pattern structure).</p> <p>See also the related answers here for the question <a href="https://stackoverflow.com/questions/456672/class-factory-in-python">Class factory in Python</a>. Also see <a href="http://www.drdobbs.com/conversations-abstract-factory-template/184403786" rel="nofollow noreferrer"><em>Abstract Factory, Template Style</em></a> which is a 2001 Dr. Dobb's article also about implementing them with C++ Templates.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload