This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

method chaining

 
Jayashree Menon
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 166
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jayashree Menon
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic