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

method chaining

Jayashree Menon
Greenhorn

Joined: Jan 19, 2005
Posts: 16
Suppose we have something as follows:


How does the evaluation take place, if say suppose all three methods return a String?
Yosi Hendarsjah
Ranch Hand

Joined: Oct 02, 2003
Posts: 164
Hi Jayashree,

equals to

Important:
method1() must return an object that has method2()
method2() must return an object that has method3()
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
The term "method chaining" I believe is usually reserved for the case where each method returns the same object that was originally called upon. Take a look at StringBuffer for an example. Otherwise, if each method returns a different object, I think there's no special name for it.It doesn't really matter what you call it as they both work the same way in the end.
Liam Tiarnach
Ranch Hand

Joined: Aug 06, 2004
Posts: 51
Originally posted by Jayashree M:
Suppose we have something as follows:


How does the evaluation take place, if say suppose all three methods return a String?


best way to find out is to try it out...
but to help you answer your question...
the object that is returned by obj.method1() will require method2() as one of it's methods, and the object that is returned by method2 will require that method3() is one of its methods...
otherwise it will not compile...

an example to look at...

just comment out

to get the example to compile...
and you can see how it evaluated...


- Liam...<br />- ' He who never sleeps... '
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
This kind of chaining is sometimes discoraged when you're designing your own classes. Here's a story from the Portland Pattern Repsository ...

"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).)


When you say you know method1 returns an object that has method2, and method2 returns an object that has method3 you start to write The Class That Knew Too Much. Let us know if this kind of discussion sounds interesting. We got a million of em.
[ February 15, 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
Jayashree Menon
Greenhorn

Joined: Jan 19, 2005
Posts: 16
Hello Everybody
Thanks a lot for the information.
I was going thru a few legacy coe at the place I Work and
they a lot of such method calls...I couln't figure it out initially.
Now it is clear and I know how to go about it.
Thanks again
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: method chaining