Alan Gauld wrote:
But if you want an OOP approach thre are some things to try.
First you can create a BuildingFactory class that has a
single instance (or indeed no instances because you could
use a static method... or get really fancy and create a
meta-class!).

or make it a static method of Building or get really simple and use a module-level function...

The factory class then takes the XML string
fragment and figures out which subclass of Building to
create and returns the required object. You can avoid
having to recode the factory class each time by using a
dictionary object and each subclass definition adds an entry
to the Factory class dictionary - possibly by calling a
class method of Building in the __init__() code


class Shack(Building): def __init__(self,p1,p2...): Building.__init__(self,...) Building.register(self,'Shack', Shack) ...rest of init...

I think you want to register Shack before you ever create one. You could do it after Shack is defined:

class Shack(Building):
   def __init__(self,p1,p2...):
      Building.__init__(self,...)
      ...rest of init...

Building.register('Shack', Shack)

or you could probably get tricky and do it in a metaclass...but I'm not that 
tricky.

Kent

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to