Factories in Python

Discussion in 'Python' started by Robert Ferrell, Jul 29, 2003.

  1. I'm wondering how Python cognoscenti use/implement factory patterns in
    Python. In my limited C++ experience, one reason to use a factory
    method is to make it easier to create instances of classes derived off
    a base class. The instance is declared to be an instance of the base
    class. The factory returns an instance of a derived class, and
    everything is groovy. In Python, since type declarations need not be
    static, it seems there is no need to have a factory method.

    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?

    thanks
    -robert
     
    Robert Ferrell, Jul 29, 2003
    #1
    1. Advertising

  2. Robert Ferrell

    Dave Kuhlman Guest

    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
     
    Dave Kuhlman, Jul 29, 2003
    #2
    1. Advertising

  3. Robert Ferrell

    Troy Melhase Guest

    Dave Kuhlman wrote:
    > I'd be interested in other reasons for using factories.


    My most frequent reason for using factories is to insulate client code from
    refering directly to a class name. I find I do this most often when I have
    code that's likely to change soon or change frequently. This approach
    allows me to leave client code unchanged although the actual class names
    and implementations may vary greatly.

    Consider:

    class MightBeWhatIWant:
    pass

    class HelperForThat:
    pass

    def build():
    return MightBeWhatIWant()

    As the problem is revealed thru elbow grease, this might change:

    class ThisIsWhatIReallyNeeded:
    pass

    class SomethingIMissedEarlier:
    pass

    def build():
    return ThisIsWhatIReallyNeeded()

    Granted, these aren't factories in the strictest GoF sense, but I think the
    intent is clear.

    troy
     
    Troy Melhase, Jul 29, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roedy Green

    overridding factories.

    Roedy Green, Aug 24, 2003, in forum: Java
    Replies:
    0
    Views:
    365
    Roedy Green
    Aug 24, 2003
  2. Kai Grossjohann

    JSP: Beans from factories?

    Kai Grossjohann, Dec 17, 2003, in forum: Java
    Replies:
    2
    Views:
    698
    Kai Grossjohann
    Dec 17, 2003
  3. Timo Nentwig
    Replies:
    0
    Views:
    3,935
    Timo Nentwig
    Apr 27, 2004
  4. Craig Ringer
    Replies:
    0
    Views:
    365
    Craig Ringer
    Dec 5, 2004
  5. Replies:
    1
    Views:
    452
    Oliver Wong
    Oct 2, 2006
Loading...

Share This Page