Helper classes design question

Jean-Michel Pichavant jeanmichel at sequans.com
Tue Aug 24 13:37:52 CEST 2010


John O'Hagan wrote:
> I want to know the best way to organise a bunch of functions designed to 
> operate on instances of a given class without cluttering the class itself with 
> a bunch of unrelated methods.
>
> What I've done is make what I think are called helper classes, each of which 
> are initialized with an instance of the main class and has methods which are 
> all of the same type (insofar as they return a boolean, or modify the object 
> in place, or whatever). 
>
> I'm not sure if I'm on the right track here design-wise. Maybe this could be 
> better done with inheritance (not my forte), but my first thought is that no, 
> the helper classes (if that's what they are) are not actually a type of the 
> main class, but are auxiliary to it. 
>   

I've seen the following pattern in someone else code which was working 
pretty well:
It was using composition instead of inheritance.

When a method is not found in the 'main class', then it searches within 
all the registered helpers and automatically call the helper method if 
it exists.
It was pretty much clever but has some drawbacks:
- you have to handle collisions between helpers methods name
- reading the code may become difficult because you're successfully 
calling methods that do not belong the the instance class.

JM



More information about the Python-list mailing list