Forums Register Login

method chaining

+Pie Number of slices to send: Send
Suppose we have something as follows:


How does the evaluation take place, if say suppose all three methods return a String?
+Pie Number of slices to send: Send
Hi Jayashree,

equals to

Important:
method1() must return an object that has method2()
method2() must return an object that has method3()
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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...
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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
Brace yourself while corporate america tries to sell us its things. Some day they will chill and use tiny ads.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 8671 times.
Similar Threads
instantiating class issue
Macro vs Micro commits
Hibernate error when loading JSP
Program help
Arrays
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 05:45:32.