# This code is posted for the purpose of conversation. If it is of some # value to someone that would be great. But what I hope is that the code # sparks conversations about what I did in this code and why I did it. Since # the list seems thick with OOP questions at the moment, I thought this might # be relevant. Digest and enjoy.
class Item ( object ): def __init__( self ): self._FullName = '' self._Recovery = 0 self._Exporter = SimpleItemExporter (); def SetFullName( self, name ): self._FullName = name def SetRecovery( self, value ): self._Recovery = value def SetExporter( self, exporter ): self._Exporter = exporter def Export( self ): properties = { 'FullName':self._FullName, 'Recovery':self._Recovery } self._Exporter.Export( properties ) class ItemCollection( object ): def __init__( self ): self._ItemList = [] self._CollectionIndex = -1 self._Loader = None def Add ( self, item ): self._ItemList.append(item) def MoveFirst ( self ): self._CollectionIndex = -1 def MoveNext ( self ): self._CollectionIndex += 1 if self._CollectionIndex < ( len ( self._ItemList ) ): return self._ItemList[ self._CollectionIndex ] return None def MoveTo ( self, index ): pass def GetLength ( self ): pass def SetCollectionLoader ( self, loader ): self._Loader = loader def Load ( self ): if self._Loader <> None: self._Loader.LoadCollection(self) class SimpleItemExporter ( object ): def __init__( self ): pass def Export ( self, dictionary ): print "The recovery status of " +str(dictionary['FullName'])+ " is: " + str(dictionary['Recovery']) class XMLExporter ( object ): def __init__ ( self, tag='XML' ): self.SetRootTag( tag ) def SetRootTag ( self, tag ): self._RootTag = tag; def Export ( self, dictionary ): xml = "" keys = dictionary.keys() for key in keys: xml = xml + "\n\t<" + key + ">" + str(dictionary[key]) + "</" + key + ">" print "<" + self._RootTag + ">" + xml + "\n</" + self._RootTag + ">" class SQLExporter ( object ): def __init__ ( self, tableName): self._TableName = tableName def Export ( self, dictionary ): value = "" field = "" first = 1 keys = dictionary.keys() for key in keys: if first <> 1: field = field + ", " value = value + ", " field = field + " " + key value = value + "'" + str(dictionary[key]) + "'" first = 0 print "INSERT into " + self._TableName + " (" + field + ") values (" + value + ")" class ItemCollectionLoader ( object ): def LoadCollection(self, collection): for i in range ( 1, 5 ): item = Item() item.SetFullName( 'Ron The Dude' ) item.SetRecovery( i ) collection.Add( item ) more = 1 collection = ItemCollection() # here we create a loader obbject loader = ItemCollectionLoader() # this loader object could do other things instead of the contrived example # shown here. Such as the loader object could read from a file, or a network # resource, stdin, whatever. As long as it's LoadCollection interface doesn't # change the object could be many things. I've decided to illustrate this concept # later in the code with the exporter object. collection.SetCollectionLoader(loader) collection.Load() collection.MoveFirst while more <> None: item = collection.MoveNext () if item <> None: # What's important here to note is that the exporter may have different # constructors but, the Export method takes the same arguments in each # export class. In other words, it's interface is the same. So, we # can easily switch one exporter object for another. With a little more # code you could even export these items in more than one format (if needed). #exporter = SimpleItemExporter() #exporter = XMLExporter("item") exporter = SQLExporter("itemTable") item.SetExporter(exporter) item.Export () else: more = None _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor