views:

66

answers:

2

i have come across fluent api while studying DSLs. i have searched alot on FLUENT API..the basic conclusion which i could draw out was that fluent api uses method chaining in order to make the code fluent. but i cannot understand that in object oriented languages we can always create an object and can call the methods related to it. then how is FLUENT api different from it? what other features does fluent api add?

A: 

With a fluent interface you write methods that return the object that the method was invoked on (usually self or this) and handle traditional return values as a state change in that object. If you look at say some of the Javascript libraries that use a fluent interface it makes it far easier to deal with lists and nulls as they can be handled the same way you would a single object. The disadvantage of fluent interfaces is that they tend to create monolithic god objects that have a whole heap of responsibilities.

I wouldn't want them to be used everywhere (because of the god object problem) but they are nice from time to time.

CurtainDog
+1  A: 

Your question is answered in the originating Fluent Interface blog post by Martin Fowler. The point is that the fluency in fluent API comes from the domain of a domain specific language, not only method chaining.

Gabriel Ščerbák