File Paramaterized Abstract Factory

Charles Krug cdkrug at aol.com
Tue Jan 10 23:07:30 EST 2006


List:

I have an idea for an abstract factory that parameterizes the factory
with an input module.

The general ideom can be expressed thus:

class ThingFactory(object):
    def CreateThisThing() : return ThisThing()
    def CreateThatThing() : return ThatThing()
    def CreateTheOtherThing() : return TheOtherThing()

With subclasses:

class HerThingFactory(ThingFactory):
    def CreateThisThing() : return HerThisThing()
    # etc

You'd invoke it in the usual way:

ThingClient(Factory=ThingFactory)

And Thing client would refer to:

    ThingFactory.CreateThisThing()

Now, Pythonically speaking, as the envisioned application uses only one
ThingFactory at a time, I'd ordinarily want to just load a module
(HisFactory, HerFactory, KidsFactory, YadaYadaYadaFactory) that
implements the expected interface--in this case, supplying the CreateX
methods.

What I'd like is to do something like this:

factoryFile = sys.argv[1] # we assume that argv[1] implements a
                          # correct ThingMaker interface.

# A Miracle Occurs Here

UseFactory(factoryObject)

Where UseFactory looks like:

def UseFactory(factoryObject):

    factoryObject.CreateThisThing() # returns the kind of thing


Rather a lot of preamble to learn what I'm trying to do.

Anywho, my problem is with the whole Miracle thing.

I've tried using __import__:

a = 'HerFactory'
__import(a)

Which returns:

<module 'HerFactory' from 'HerFactory.py'>

But after that I can't access the members.

Clearly I'm missing a step or seventeen.

What's the best way to do this?

Thanks


Charles





More information about the Python-list mailing list