Design Question

R

Robert Ferrell

I have a question which is more about OO design than Python per se.
I've noticed that a number of posters to this list have very good
suggestions for SE issues, so I'm hoping that someone will be able to
give me some guidance.

My question is about how to architect a class hierarchy where some
methods are nearly identical between a superclass and a subclass, but
differ slightly. For example:

class Sup(object):
def __init__(self):
self.specialFlag = False
def aMeth(self):
<do some stuff>
<if self.specialFlag, do a special thing>
<do more stuff>

class Sub(Sup):
def __init__(self):
self.specialFlag = True

In this example, the method aMeth just checks for specialFlag, and if
it's True, does the special stuff required.

This allows me to share aMeth, and not have to duplicate code.
However, this doesn't feel great to me because Sup has to know about
Sub in some fashion. If later I add other subclasses with their own
special needs, the entanglement will just get worse.

An alternative I thought of was creating a module with helper
functions. That module would contain a function which implements
whatever aMeth does, and knos about the special needs of Sub. Both
Sup and Sub would invoke the helper method. That allows me to share
code, avoids a direct dependency of Sup on Sub, but does add a new
module.

I'm guessing that there is some well known pattern that addresses this
issue, but I don't know what it is. Does anyone have suggestions for
architecture that provides a good solution?

thanks,
-robert
 
M

Michael Hudson

I have a question which is more about OO design than Python per se.
I've noticed that a number of posters to this list have very good
suggestions for SE issues, so I'm hoping that someone will be able to
give me some guidance.

My question is about how to architect a class hierarchy where some
methods are nearly identical between a superclass and a subclass, but
differ slightly. For example:

class Sup(object):
def __init__(self):
self.specialFlag = False
def aMeth(self):
<do some stuff>
<if self.specialFlag, do a special thing>
<do more stuff>

class Sub(Sup):
def __init__(self):
self.specialFlag = True

In this example, the method aMeth just checks for specialFlag, and if
it's True, does the special stuff required.

This allows me to share aMeth, and not have to duplicate code.
However, this doesn't feel great to me because Sup has to know about
Sub in some fashion.

I'm not an OO bigot, and in some circumstances I think that
superclasses knowing about all their subclasses is just fine.
If later I add other subclasses with their own special needs, the
entanglement will just get worse.

If you do this, then yes. Are you going to? (Yes, you can feel an XP
flavour to my thoughts...).

The idea that sometimes seems to appear of almost doing battle with
your subclasses seems particularly misguided.

[...]
I'm guessing that there is some well known pattern that addresses this
issue, but I don't know what it is. Does anyone have suggestions for
architecture that provides a good solution?

well,

class Sup(object):
def __init__(self):
self.specialFlag = False
def aMeth(self):
<do some stuff>
self.doSpecialThing()
<do more stuff>
def doSpecialThing(self):
pass

class Sub(Sup):
def doSpecialThing(self):
<do a special thing>

kind of suggests itself, but it's hard to say out of context whether
it applies. You might enjoy reading Alex Martelli's talk on the
template design pattern that he gave at EuroPython 2003:

http://www.strakt.com/docs/ep03_pydp.pdf

Cheers,
mwh
 
B

Bernhard Herzog

My question is about how to architect a class hierarchy where some
methods are nearly identical between a superclass and a subclass, but
differ slightly. For example:

class Sup(object):
def __init__(self):
self.specialFlag = False
def aMeth(self):
<do some stuff>
<if self.specialFlag, do a special thing>
<do more stuff>

class Sub(Sup):
def __init__(self):
self.specialFlag = True

A commonly used alternative would be

class Sup(object):
def aMeth(self):
<do some stuff>
self.specialMethod()
<do more stuff>
def specialMethod(self):
pass

class Sub(Sup):
def specialMethod(self):
<do a special thing>



Bernhard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,944
Members
47,491
Latest member
mohitk

Latest Threads

Top