Factories in Python

Dave Kuhlman dkuhlman at rexx.com
Tue Jul 29 20:24:49 CEST 2003


Robert Ferrell wrote:

[snip]
> 
> However, I'm guessing that I'm not understanding other benefits of
> using factories in Python.  Does anybody have examples or pointers
> on how use of factories in Python?

Here is a link:

    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/86900

A reason for using a factory is that the client (the code that
creates the instance) does not know which class or sub-class it
wants to create an instance of.  So, you build a factory that is
smart enough to create an instance of the right class.

In my case, while parsing an XML document and creating instances
of nodes in the tree that represents the document, the classes
(the super-class of each node class) does the parsing.  However,
each super-class must be smart enough to create a tree populated
with instances of sub-classes, which may contain additional custom
code,*if* they are available.

I'd be interested in other reasons for using factories.

 - Dave

-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
dkuhlman at rexx.com




More information about the Python-list mailing list