Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Function return Type of its own class  RSS feed

 
Anshul Singhal
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have this (final)class named ConfigurationBuilder. This Class has some methods all of them have return type ConfigurationBuilder




so why does writing



work???


shouldn't there be some variable to handle the return type??
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing says that you have to capture a returned value.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anshul Singhal wrote:shouldn't there be some variable to handle the return type??

No. Methods can always be chained, eg:
String trimmedSubstr = someString.subString(10, 99).trim();
as long as the next method in sequence is valid for the value returned by previous one.

However, what you're seeing is a style used by a lot of Builder implementations, called a Fluent Interface.
It hasn't really caught on in a major way generally, because good FIs are tough to design (and easy to get wrong ); but it does dovetail quite nicely with the Builder pattern.

HIH

Winston
 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Speaking of fluent interface, I once wrote a Move class for a chess program, that had an internal Builder class. You created an instance by calling Move.from("A6").to("A4"); or something similar, with the static from() method returning a new Builder object. It was just an exercise for me, but I still don't know whether I loved or hated that code more :P
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Speaking of fluent interface, I once wrote a Move class for a chess program, that had an internal Builder class. You created an instance by calling Move.from("A6").to("A4"); or something similar, with the static from() method returning a new Builder object. It was just an exercise for me, but I still don't know whether I loved or hated that code more :P

Hmmm. Interesting.

Actually, FIs have always quite interested me; although I've never attempted an industrial strength one of my own yet. When I was on OTN, there was a guy who had developed one for use with regexes which looked pretty decent to me; and I've always thought that there might be some application for them with Java collections.

Another one for when I've got the time...

Winston
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've recently written quite a simple one - switching an application from a complicated XML configuration to a Fluent interface. It's much easier to read, and has the advantage of type safety (the configuration involves instantiating objects of arbitrary types that implement a particular interface).

It was quite an interesting exercise to work "in reverse": I wrote the configuration code as I wanted it to work, to keep it as clean as possible, and then wrote the Fluent interface to match.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!