Puzzling OO design problem
George Sakkis
gsakkis at rutgers.edu
Fri Apr 8 21:40:54 EDT 2005
> Err, you might want to explain what these things do instead of an
> abstract description of how you are doing it. It looks like you are
> using inheritance in the normal way _and_ you are using it to handle
> versioning of some kind (maybe stable interface releases? I don't
know).
>
> Let us know what parts need inheritance for, and what you have
> just been using a side effect of inheritance for as a convenience
> (versioning, I think).
>
> A more concrete example would be easier to comment on, if possible
> do a simple one (maybe just two classes with two versions each).
>
> -jackdied
I intentionally abstracted the problem to remove the irrelevant
details, but here's a more concrete (though still simplified) example.
I hope it is more clear now.
George
#=============================
def worldModelFactory(version):
if version < 2: return WorldModel()
else: return WorldModel_v2()
class WorldModel(object):
def __init__(self):
self.ourGoal = self.FieldObject(x=-50, y=0)
self.theirGoal = self.FieldObject(x=+50, y=0)
self.ball = self.MovableObject()
self.teammates = [self.Player(i) for i in xrange(1,12)]
self.opponents = [self.Player(i) for i in xrange(1,12)]
class FieldObject(object):
def __init__(self, id=None, x=0, y=0):
self.id = id
self._pos = (x,y)
def position(self):
'''Get or estimate the current position.'''
return self._pos
class MovableObject(FieldObject):
def speed(self):
'''Get or estimate the current speed.'''
# [implementation snipped]
class Player(MovableObject):
def passBall(self,power,teammate):
'''Pass the ball to the teammate.'''
# [implementation snipped]
class WorldModel_v2(WorldModel):
class FieldObject(WorldModel.FieldObject):
'''New implementation of FieldObject.'''
def position(self):
# [new implementation snipped]
pass
class MovableObject(WorldModel.MovableObject, FieldObject):
'''MovableObject didn't change since the previous version. The
only reason for this class is to make
WorldModel_v2.FieldObject
accessible to WorldModel_v2.Player.
'''
pass
class Player(WorldModel.Player, MovableObject):
'''New implementation of Player.'''
def passBall(self,power,teammate):
# WorldModel_v2.FieldObject.position() should be called
myPosition = self.position()
# [new implementation snipped]
#=============================
More information about the Python-list
mailing list