[Tutor] Object Oriented References? and a couple of question s
<--LONG
alan.gauld@bt.com
alan.gauld@bt.com
Sat, 11 Aug 2001 19:25:56 +0100
> I'm trying to hack my way into the verdant valley that is
> Pythonic Object Orientism.
> Would anybody have any recommendations for info on the subject?
As usual i'll recommend my tutor :-)
http://www.crosswinds.net/~agauld
Look for the OO topic then also read the OO section of
the case study.
> I've got a Cook who will scan a directory for ingredients, sift them
> so that we are using only the ingredients we want, and then cook up a
> bunch of Python Objects. Then I've got a Host who will deal with the
> Diners and handle all of their requests and make sure
> everyone gets what
> they need. I've also got a number of Recipies (object templates) for
> The Cook to use. This is how I'm thinking I'll lay things out, let me
> know if this isn't the best OO way to do things.
Thats a problem statement. Remember that in general "people"
make bad objects. So try to avoid the Cook, Host and Diners
as objects at least initially. They may have a role to play as "control"
objects later.
That leaves: ingredients, requests, recipies.
> The Cook will first scan a directory, and make a list of all
> the files. This will be his Pantry.
Oh another object - the pantry.
> He will then Sift through the pantry for the files required
> by the Recipe.
Better to get the pantry to do its own sifting - it owns
the ingredients, it is responsible for operating on them
- a key principle of OOD is he who owns the data does
the work... Sometimes called "Responsibility Driven Design".
> that will be refered to as the Ingredients. These Ingredients will be
> used to Bake a bunch of objects that I plan to use later.
Which objects? Meals maybe? Each meal knows how to bake itself maybe?
> When I'm setting up the Cook class I'm thinking the gather(), sift(),
> and bake() will be methods of the class,
No these are methods of the other classes that the cook controls by calling
on them.
Thus
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)
> I'm really foggy on how to properly use classes to create objects and
> then use those objects. As you can see, I'm in dire need of
> a good explanation.
Try Timothy Budd's OO Programming. He uses 5 languages to
explain the key principles of OO - showing how each language
has its own foibles but the underlying principles are the same...
HTH,
Alan G