tags:

views:

108

answers:

1

I'm having a hard time making a design decision

I have a class in python, that processing form data, this data is very similar to other form data, and so I'm refactoring it into it's own object so it can be reused by the other classes.

The delima is weather to make this formprocessor a member of the classes or a parent of the classes.

please correct me if this terminology is wrong, here is what I'm torn between:

monolithic inheritance based classes:

class FormProcessor(object):
    def post(self):
        # ... process form data

class PageHandler(RequestHandler,FormProcessor):
    def get(self):
        # show page

or the more modular member based classes:

class FormProcessor(object):
    def process(self):
        # ... process form data

class PageHandler(RequestHandler):
    def __init__(self):
        self.processor = FormProcessor()
    def get(self):
        # show page

    def post(self):
        self.processor.process(self.postdata)

I'm leaning toward the second but I'm not sure what the consequences could be down the road in terms of maintainability. The reason I'm leaning toward it is that, I like to think of the processing as an action that takes place, not as a main part of the PageHandler, so this makes sense to make it a member object, instead of a parent.

It bothers me that when classes interit functionality it ends up with a very long list of functions on an instance, I'm trying to categorize them better so the program is understandable and reflects the system that it is modeling.

look forward to any advice on this issue

+6  A: 

Definitely go for the modular approach. Some of the advantages of taking the modular approach are:

  • It makes your code more readable, i.e. it's more clear what the PageHandler and FormProcessor do
  • It makes it easier and more effective to write unit tests on both of your classes
  • It makes it easier to change the behavior of PageHandler at a later date by using a different implementation of PageHandler

In general, stick to the idea that one class does one thing; it's easier to see what you're working with when maintaining your software, as well as making it easier to write (you only have to think about one context at a time then).

gab