Difference between method chaining and fluent interface

Viewed 1835

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?

3 Answers

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.

Related