What exactly is a Class Factory?

2019-01-08 15:14发布

问题:

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



回答2:

Heres's a little 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 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.

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.

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 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 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). Update: I located a 1998 paper he 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).

See also the related answers here for the question Class factory in Python. Also see Abstract Factory, Template Style which is a 2001 Dr. Dobb's article also about implementing them with C++ Templates.



回答3:

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



回答4:

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.



回答5:

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



标签: class factory