• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Objects changing other objects?

 
Michael Wexler
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This continues to confuse me. I get that objects have properties, so dog has legs=4 or color=black. But if I tell dog to bite (method), doesn't the dog need a "target" to bite? That is, how do I tell the dog to bite the man?

If I were coding, would it be something like dog.bite(man)?

How does the state of man change? Is this where all that "message" stuff happens, that a message would get sent to the "man" object changing its, say, health property from "good" to "hurt"?

All the examples I read talk about objects changing themselves, or objects calling methods of other objects. But how do we get objects to interact amongst themselves?

If this is too hard or too random, I apologize. It just really bugs me that I get so confused about this.

Thanks, Michael
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,

It sounds to me like you actually have a good technical understanding of things already -- but perhaps not the practical experience of seeing how it's used. Here's some of your ideas sketched out as real code:



The World creates a Dog and a Man, and contrives that the Dog should see the Man. The Dog then decides (using code not shown here) whether to bite the Man. If it does bite him, it inflicts a certain amount of damage. It's the Man's job to adjust his health based on this damage, and possibly die (again, invoking a method I haven't shown).


Does this help?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest, nice example!
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This continues to confuse me. I get that objects have properties, so dog has legs=4 or color=black. But if I tell dog to bite (method), doesn't the dog need a "target" to bite? That is, how do I tell the dog to bite the man?

There might also be some methods that don't need a 'target', for example inABadMood() in the code above, or goToSleep().
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great question and neat example. It's generally a good thing if you just tell objects what to do and they know how to do the work without revealing the details. The alternative is asking objects for data and doing the work yourself which leads to nasty coupling. Some contrast this as methods that "command" vs "query." Knight's Principles has more light-hearted advice about such things.

And since YOU brought up dogs & legs I get to drag this out ...

Don't Confuse Your Dog

Quoted from the Portland Pattern Repository a.k.a. The WikiWikiWeb

"If you want your dog to run, do you talk [to] your dog or to each leg? Further, should you be able to manipulate the dog's leg without it knowing about it? What if your dog wants to move its leg and it doesn't know how you left it? You can really confuse your dog."

The moral: Change the state of a contained object only through the containing object's interface.

"Aha!" you say. "If I employ the technique of encapsulation, then a method won't even know of the existence of any contained objects."

That's precisely the point. To walk your dog, you don't need to know of the existence of its legs. (And in fact, dogs with fewer than four legs can go for walks. There is even a case of a dog whose hindquarters rested on a little carriage; its "walk" method employed two legs and two wheels (these latter, one hopes, encapsulated within a "carriage" interface).)
[ May 01, 2005: Message edited by: Stan James ]
 
Neeraj Dheer
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
really innovative example!! very different and definitely a lot more interesting than the 'car' example i always use!!

and very well used too!!!
 
Michael Wexler
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, all. This has started to help me out. But I still think I need to meditate on objects some more.

The biggest thing, I think, is that even if you encapsulate everything, at some level, objects have to know what methods are fair game in other objects. Therefore, the dog biting man is really the dog calling the appropriate method on the man object. The programmer has to know all those methods (or have them documented somewhere) to really use the object.

Thanks again for some well-thought out answers...

Best, Michael
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Wexler:

The biggest thing, I think, is that even if you encapsulate everything, at some level, objects have to know what methods are fair game in other objects. Therefore, the dog biting man is really the dog calling the appropriate method on the man object. The programmer has to know all those methods (or have them documented somewhere) to really use the object.


Or perhaps the programmer's tools know all the methods! These days, most programmers work in an IDE which can find out all this information, and give it to the programmer as needed.

But yes, that's right. The set of methods an object has is, in some sense, everything you can know about it. There's a special term for this: it's called the class's "public interface".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic