tags:

views:

198

answers:

5

I see the word thrown around often, and I may have used it myself in code and libraries over time, but I never really got it. In most write-ups I came across, they just went on expecting you to figure it out.

What is a Class Factory? Can someone explain the concept?

+1  A: 

A class factory is a method which (according to some parameters for example) returns you a customised class (not instantiated!).

ChristopheD
+1  A: 

I felt that this explains it pretty well (for me, anyway). Class factories are used in the factory design pattern, I think.

Like other creational patterns, it [the factory design pattern] deals with the problem of creating objects (products) without specifying the exact class of object that will be created. The factory method design pattern handles this problem by defining a separate method for creating the objects, which subclasses can then override to specify the derived type of product that will be created. More generally, the term factory method is often used to refer to any method whose main purpose is creation of objects.

http://en.wikipedia.org/wiki/Factory_method_pattern

Apologies if you've already read this and found it to be insufficient.

Matt H
+2  A: 

A class factory constructs instances of other classes. Typically, the classes they create share a common base class or interface, but derived classes are returned.

For example, you could have a class factory that took a database connection string and returned a class implementing IDbConnection such as SqlConnection (class and interface from .Net)

Rowland Shaw
+1  A: 

The Wikipedia article gives a pretty good definition: http://en.wikipedia.org/wiki/Factory_pattern

But probably the most authoritative definition would be found in the Design Patterns book by Gamma et al. (commonly called the Gang of Four Book).

Will
+4  A: 

Here some additional information that may help better understand some of the other technically correct, but shorter 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 combined-effect 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 needs to be simulated because constructors cannot really be virtual in C++, which is why such object (not class) factories are usually implemented as standalone functions or static methods.

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).

James Coplien's 1992 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 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).

See also the related answers for the question "Class factory in Python" here.

martineau