aspose file tools*
The moose likes Beginning Java and the fly likes Objects changing other objects? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Objects changing other objects?" Watch "Objects changing other objects?" New topic
Author

Objects changing other objects?

Michael Wexler
Greenhorn

Joined: Apr 06, 2004
Posts: 10
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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?


[Jess in Action][AskingGoodQuestions]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Ernest, nice example!


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Stuart Gray
Ranch Hand

Joined: Apr 21, 2005
Posts: 410

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

Joined: Jan 29, 2003
Posts: 8791
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 ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
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

Joined: Apr 06, 2004
Posts: 10
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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".
 
Don't get me started about those stupid light bulbs.
 
subject: Objects changing other objects?