[Tutor] Object Oriented References? and a couple of question s
<--LONG
alan.gauld@bt.com
alan.gauld@bt.com
Mon, 13 Aug 2001 11:34:41 +0100
> On Saturday, August 11, 2001, at 11:25 AM, alan.gauld@bt.com wrote:
> > cook = Cook()
> > pantry = Pantry()
> > cook.makeMeal()
> >
> > And the makeMeal method might look like this:
> >
> > class Cook:
> > def makeMeal(meal)
> > ingredients = pantry.gather()
> > for item in ingredients
> > item.sift()
> > return meal.bake(ingredients)
>
> Don't you lose polymorphism by hard-coding a call to a specific
> instance's method?
I'm not intending to limit it because pantry is set external to the
method. It might in fact be an attribute of the Cook class
(or maybe the Kitchen class within which the cook instance
is currently working ;-). Thus whatever pantry is instantiated
to - it could be a Cupboard or a Freezer or a JITDeliveryservice
type of "pantry" the methods will polymorphically function
correctly. It probably would be a bad idea to instantiate
the pantry within the makeMeal method.
> Wouldn't it be more consistent with OO design to
> allow "makeMeal" to call the "gather" method of any object that
> implements it?
Thats what I did, I just didn't specify where/how the pantry
should be set.
> class Cook:
> def makeMeal(self, meal, supplier):
> ingredients = supplier.gather(meal)
> . . .
I would probably prefere to put supplier as an attribute
of Cook set in the __init__ method say. But the principle
is right.
> lunch = cook1.makeMeal("tuna fish sandwich", source1)
I'd suggest any intelligent cook should know where to
find his ingredients, he wouldn't expect the diner to
tell him!
But there's no absolute in this without knowing an awful
lot more about the requirements...
Alan G.