Reputation: 475
I wanted to know the exact difference between method chaining and fluent interface. As I understand it, method chaining is just running the methods of previous method return objects while avoiding temporary variables. An example of this could be
Integer.parseInt(str).intValue()
With respect to fluent interface, each method of the object is chained by a point, without having to be related to the previous method The two techniques make modifiers methods return to host objects, so that multiple modifiers can be invoked in a single expression like this:
new Car().StartsEngine().OpenWindow()
is this correct?
Upvotes: 5
Views: 3812
Reputation: 17104
The term Fluent Interface was coined by Martin Fowler and Eric Evans. Fowler says,
Probably the most important thing to notice about this style is that the intent is to do something along the lines of an internal Domain Specific Language. Indeed this is why we chose the term 'fluent' to describe it, in many ways the two terms are synonyms.
...
I've also noticed a common misconception - many people seem to equate fluent interfaces with Method Chaining. Certainly chaining is a common technique to use with fluent interfaces, but true fluency is much more than that.
Fowler references Fluent Interfaces when discussing DSLs as well.
Internal DSLs are also referred to as embedded DSLs or Fluent Interfaces.
So it seems that, to describe Fluent Interfaces, Fowler needs to describe (internal) DSLs.
For internal DSLs, the fuzzy boundary is what is an API and what is a DSL. Fundamentally there is no difference, an internal DSL is just an API with a fancy name... Despite this, however, I think there is a different feel when you are working with an API that's written with a DSL feel. Things like a Fluent Interface can make working with an API a qualitatively different experience. Thinking in DSL terms makes you think about readability in a different way...
In my own experience Fluent Interfaces always employ Method Chaining; but that is just one of many DSL Patterns. In addition, I think we've all seen method chains that detract from readability; so a chain of calls is not automatically fluent.
I would say the primary difference is syntax vs semantics.
Upvotes: 4
Reputation: 845
Fluent interfaces can be achieved through method chaining, but not all method chains are Fluent interfaces. In Fluent interfaces, method chaining always returns the same interface which is used by all chaining methods. For example:
public interface Car
{
Car StartEngine();
Car OpenWindows();
Car CloseWindows();
Car startAC();
}
Now Fluent interfaces is implemented with chaining as follows:
Car hondaCity = new HondaCity();
hondaCity.startEngine().openWindows().closeWindows().startAC();
Now simple method Chaining example:
Car hondaCity = new HondaCity();
hondaCity.getEngine(). //Get Engine Object
getFilter(). // get Filter Object
cleanFilter();
Upvotes: 2
Reputation: 21757
In my opinion, if we ignore the type system, fluent interface is exactly the same as method chaining.
However, fluent interface is more about utilizing the type system on the return type to constrain the next potential chaining method candidates.
In a cleverly designed DSL with fluent interface, each method invocation would modify the return type so as to allow a small set of logically appropriate methods to be chained next (a bit like state machine). Hence the control flow is validated in compile time.
Upvotes: 0
Reputation: 39980
I don't think there's a major difference; or rather, the two concepts are at different layers. Method chaining is the simple thing where you call a method directly on the return value of a different method.
A fluent interface is a style of designing an API to do a multiple-step, complex operation so that it reads close to prose. A fluent interface will be meant to be used through method chaining. It can use the same mutable object for every call, or it can return a new immutable object every time, depending on what the interface author thinks is a good idea.
Upvotes: 7