16

I recently came across this expression - but reading up on Wikipedia did not clarify it much for me - I still don't get it:

  1. What's the point of it
  2. How is it used in practice (i.e. how does it benefit a coder in their day to day work/building systems)?

[Edit] The Wikipedia article C++ example is overly long, and conflates the discussion of a fluent interface with an example of a simple Glut app. Can someone provide a SUCCINCT C++ example of a class that illustrates a fluent interface (how does such an influence differ from an ordinary C++ interface for example)?

John Topley
  • 113,588
  • 46
  • 195
  • 237
Stick it to THE MAN
  • 5,621
  • 17
  • 77
  • 93

4 Answers4

14

It benefits the coder by reducing the amount he has to type (and read).

To use the C++ example on Wikipedia:

Before:

int main(int argc, char **argv) {
     GlutApp app(argc, argv);
     app.setDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_ALPHA|GLUT_DEPTH); // Set framebuffer params
     app.setWindowSize(500, 500); // Set window params
     app.setWindowPosition(200, 200);
     app.setTitle("My OpenGL/GLUT App");
     app.create();
}

After:

 int main(int argc, char **argv) {
     FluentGlutApp app(argc, argv);
     app.withDoubleBuffer().withRGBA().withAlpha().withDepth()
        .at(200, 200).across(500, 500)
        .named("My OpenGL/GLUT App");
     app.create();
 }
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • Could you provide a succinct example of this?. The wikipedia article C++ example is overly long, and the point of the fluent interface discussion is lost in the waffle of a Glut application. – Stick it to THE MAN Jan 18 '10 at 07:47
  • 1
    How does this differ from method chaining? (is this another case of someone coining a new phrase to describe something that is already known by another name?) – Stick it to THE MAN Jan 18 '10 at 07:54
  • 4
    @Stick it to THE MAN: The wikipedia article links to Martin Fowler's original article (where he coined the phrase). At the very end of that article, he gives some indication of why fluent interfaces are more than just method chaining. http://martinfowler.com/bliki/FluentInterface.html – e.James Jan 18 '10 at 08:11
  • 1
    To sum it up: a fluent interface *makes use of* method chaining, among other tools, in order to make interaction with that interface read more naturally to the class user. The result is code that looks very much like a domain-specific language. – e.James Jan 18 '10 at 08:13
  • 1
    To see what makes Thilo's second example more "fluent", try reading it out loud. It reads, at least to my ears, a lot like a sentence that someone would write when describing this GLUT App to a colleague: "So, I made this app with a double buffer. It had RGBA, alpha, and depth. I made the window from (200, 200) across (500, 500) and I named it 'My OpenGL/GLUT App'" – e.James Jan 18 '10 at 08:20
  • Anyway, that's my understanding of it. I hope it helps! – e.James Jan 18 '10 at 08:20
  • Re: method chaining: Note that in addition to using method chaining, the names of the methods have also changed, and the bitmask parameters have been replaced by individual setters, which makes the code read more "fluently". – Thilo Jan 18 '10 at 08:28
  • Thanks guys - Fowler's doc really helped drive the point home. – Stick it to THE MAN Jan 19 '10 at 10:22
9

There are different interpretations of the term "fluent interface". A common way to create one in C++ is method chaining, which is commonly used in for example the iostream library:

Object.MethodA().MethodB();
cout << "a = " << a;

The Named Parameter Idiom is another nice example of a fluent interface:

Window w = CreateWindow()
               .Width(400)
               .Height(300)
               .OnTop();

The benefits? Code that's better readable and more flexible, although that still depends on the implementation of course.

jbvo
  • 131
  • 4
2

One big difference and advantage of the fluent interface is that you don't need an instance variable to change some properties when you want to create an object and use it as an argument:

without:

Object object;
object.setcolor("red"); 
object.setstyle("solid");
object.setname("test");
world.CreateNode(object);

with fluent interface:

world.CreateNode(Object()
                                           .setcolor("red")
                                           .setstyle("solid")
                                           .setname("test")
                             );
Ozan
  • 4,345
  • 2
  • 23
  • 35
0

CallStream aka "Why the dot's ?" :)

Nothing wrong with a bit of (very simple) JavaScript to describe new and important concepts. Perfectly doable in C++, too.cept

Key concept: using Functional Programming idiom, describe the interface. Without using: message argument as a string, to be parsed, concept.

I think it is mostly related to the "Facade" pattern, but yes, it is related to a "Builder pattern". And it seems it survived the "Monads" too.

btw: All the advocates above have presented very good use-cases.

Chef Gladiator
  • 902
  • 11
  • 23