I recently came across this expression - but reading up on Wikipedia did not clarify it much for me - I still don't get it:
[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)?
Fluent UI React is Microsoft's official front-end React-based open-source UI framework designed to build experiences that seamlessly fit into various Microsoft products. It enables highly customizable, accessible, robust, and up-to-date components using CSS in JS.
A fluent interface provides an easy-readable, flowing interface, that often mimics a domain specific language. Using this pattern results in code that can be read nearly as human language.
Fluent interfaces are good for users, but bad for library developers. Small objects are good for developers, but difficult to understand and use. It seems to be so, but only if you are used to large classes and procedural programming.
A fluent interface is an object-oriented API that depends largely on method chaining. The goal of a fluent interface is to reduce code complexity, make the code readable, and create a domain specific language (DSL). It is a type of method chaining in which the context is maintained using a chain.
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();
}
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.
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")
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With