Factories in Python

Troy Melhase troy at gci.net
Tue Jul 29 22:54:32 CEST 2003


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




More information about the Python-list mailing list