Deeply-nested class layout suggestions wanted

Robert Brewer fumanchu at amor.org
Thu Jun 10 22:37:08 EDT 2004


Kirk Strauser wrote:
> ...I plan to subclass the heck out of each of these
> classes, with overloading appropriate to the type of data source being
> represented.  For example, a DataSource that retrieves images 
> from a POP3 mailbox might be defined like:
> 
>   POP3Retriever.py:
> [snip]
> 
> Such a class will be further subclassed into modules like 
> POP3TiffFile, POP3ZipArchive, etc., the goal being to keep all 
> functionality as high in the inheritence hierarchy as possible,
> so that the "leaf" modules define nothing more than the
> bare minimum possible to distinguish each other.  I'd
> like to carry this to the point of not defining any classes 
> that are the same between siblings (the DataSource class
> is identical between all of the different POP3Retriever
> subclasses, for example).
> 
> [1] We receive files from our customers via many means - fax, 
> email, ftp, you name it.  I'm developing delivery method
> agnostic tools to manipulate those files and flatly refuse
> to write n tools to handle n methods.

First thought: seeing two example classes named "POP3TiffFile" and
"POP3ZipArchive" makes me wonder if this isn't a better candidate for
delegation than subclassing. That is, a "POP3Retriever" would *possess*
a "ZipArchive" handler rather than *be* a ZipArchive handler. Another
instance of the same POP3Retriever class could possess a TiffFileHandler
rather than *be* a TiffFileHandler.

"""app/handlers.py"""
class FileFormatHandler(object):
    def handle(self, ...):
        ...

class TiffFileHandler(FileFormatHandler):
    def handle(self, ...):
        ...

class ZipArchiveHandler(FileFormatHandler):
    def handle(self, ...):
        ...

--------------------------

"""app/retrievers.py"""
from app import handlers

class FileRetriever(object):
    ...

class POP3Retriever(FileRetriever):
    def __init__(self, handler=handlers.ZipArchiveHandler):
        self.handler = handler


If by "keep all functionality as high in the inheritance hierarchy as
possible", you imply "keep the module heirarchy flat", then delegation
is a good technique. But I'd be interested to hear why you did not
choose it (yet ;).


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org




More information about the Python-list mailing list