[FRPythoneers] Class factory

Matt Gushee mgushee at havenrock.com
Thu Mar 28 11:46:13 MST 2002

On Thu, Mar 28, 2002 at 11:21:25AM -0700, Evelyn Mitchell wrote:

> > So - what are class factories good for? Where can I find a non-trivial example?
> Factories are an instance of a Pattern. Patterns are shorthand ways of
> describing common methods of constructing software, just as 'dovetail


> A Factory pattern describes a way of creating new objects.

No argument with any of that, but to look at it a little more concretely,
one common situation where factories are useful is when you want to create
a high-level interface that abstracts the functionality of two or more
implementations. For example, the DOM and SAX APIs for XML processing
each have several Python implementations, but unless you need features
that are only offered by a particular implementation, you want to code
to a higher-level API. So you do something like:

  impl = xml.dom.getDOMImplementation()  # calling factory function

Then (hopefully) you have an object which will respond to all the
standard DOM methods without having to worry about its underlying

If you want to see how getDOMImplementation works, look at the code
in <python_dir>/xml/dom/, especially __init__.py and domreg.py. If
you have PyXML installed, you'll find another version of it in 

The Python DB API also uses the Factory pattern, I think, though I
haven't studied the code in depth.

Hope this helps a bit.
Matt Gushee
Englewood, Colorado, USA
mgushee at havenrock.com

More information about the FRPythoneers mailing list