• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Function return Type of its own class

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nothing says that you have to capture a returned value.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic