Fluent interface
In software engineering, a fluent interface is an object-oriented API whose design relies extensively on method chaining. Its goal is to increase code legibility by creating a domain-specific language. The term was coined in 2005 by Eric Evans and Martin Fowler.
Implementation
A fluent interface is commonly implemented through method chaining to achieve method cascading in languages that do not provide cascading natively. This is typically done by having each method return the object on which it was invoked, often referred to asthis or self. More abstractly, a fluent interface maintains the instruction context of a subsequent call within a chain, where the context is generally:- defined through the return value of the preceding method,
- self-referential, where the new context is equivalent to the previous one,
- or terminated by returning a void context.
History
The term "fluent interface" was coined in late 2005, though this overall style of interface dates to the invention of method cascading in Smalltalk in the 1970s, and numerous examples in the 1980s. A common example is the iostream library in C++, which uses the<< or >> operators for the message passing, sending multiple data to the same object and allowing "manipulators" for other method calls. Other early examples include the Garnet system and the Amulet system which used this style for object creation and property assignment.Examples
C#
uses fluent programming extensively in LINQ to build queries using "standard query operators". The implementation is based on extension methods.// An English-to-French dictionary of animal names
Dictionary
// Find translations for English words containing the letter "a",
// sorted by length and displayed in uppercase
IEnumerable
.Where
.OrderBy
.Select);
// The same query constructed progressively:
IEnumerable
IEnumerable
IEnumerable
Fluent interface can also be used to chain a set of methods, which operate on/share the same object. Instead of creating a customer class, we can create a data context which can be decorated with fluent interface as follows.
// Defines the data context
class Context
class Customer
class Program
The.NET testing framework NUnit uses a mix of C#'s methods and properties in a fluent style to construct its "constraint based" assertions:
Assert.That => 2 * 2, Is.AtLeast.And.AtMost);
C++
A common use of the fluent interface in C++ is the standard iostream, which chains overloaded operators.The following is an example of providing a fluent interface wrapper on top of a more traditional interface in C++:
import std;
using std::string;
using std::vector;
// Basic definition
class GlutApp ;
// Basic usage
int main
// Fluent wrapper
class FluentGlutApp : private GlutApp ;
// Fluent usage
int main
Java
An example of a fluent test expectation in the jMock testing framework is:mock.expects).method.with,
stringContains) );
The jOOQ library models SQL as a fluent API in Java:
Author author = AUTHOR.as;
create.selectFrom
.where
.from
.where
.and
)
);
The fluflu annotation processor enables the creation of a fluent API using Java annotations.
The JaQue library enables Java 8 Lambdas to be represented as objects in the form of expression trees at runtime, making it possible to create type-safe fluent interfaces, i.e., instead of:
Customer customer = new Customer;
customer.property.eq
One can write:
method
Also, the mock object testing library EasyMock makes extensive use of this style of interface to provide an expressive programming interface.
Collection mockCollection = EasyMock.createMock;
EasyMock
.expect
.andThrow)
.atLeastOnce;
In the Java Swing API, the LayoutManager interface defines how Container objects can have controlled Component placement. One of the more powerful
LayoutManager implementations is the GridBagLayout class which requires the use of the GridBagConstraints class to specify how layout control occurs. A typical example of the use of this class is something like the following.import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
GridBagLayout gl = new GridBagLayout;
JPanel p = new JPanel;
p.setLayout;
JLabel l = new JLabel;
JTextField nm = new JTextField;
GridBagConstraints gc = new GridBagConstraints;
gc.gridx = 0;
gc.gridy = 0;
gc.fill = GridBagConstraints.NONE;
p.add;
gc.gridx = 1;
gc.fill = GridBagConstraints.HORIZONTAL;
gc.weightx = 1;
p.add;
This creates a lot of code and makes it difficult to see what exactly is happening here. The
Packer class provides a fluent mechanism, so you would instead write:import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
JPanel p = new JPanel;
Packer pk = new Packer;
JLabel l = new JLabel;
JTextField nm = new JTextField;
pk.pack.gridx.gridy;
pk.pack.gridx.gridy.fillx;
There are many places where fluent APIs can simplify how software is written and help create an API language that helps users be much more productive and comfortable with the API because the return value of a method always provides a context for further actions in that context.
JavaScript
There are many examples of JavaScript libraries that use some variant of this: jQuery probably being the most well known. Typically, fluent builders are used to implement "database queries", for example in the Dynamite client library:// getting an item from a table
client.getItem
.setHashKey
.setRangeKey
.execute
.then
A simple way to do this in JavaScript is using prototype inheritance and
this.// example from https://schier.co/blog/2013/11/14/method-chaining-in-javascript.html
class Kitten
// use it
new Kitten
.setName
.setColor
.save;
Scala
supports a fluent syntax for both method calls and class mixins, using traits and thewith keyword. For example:class Color
object Black extends Color
trait GUIWindow
trait WindowBorder extends GUIWindow
class SwingWindow extends GUIWindow
val appWin = new SwingWindow with WindowBorder
appWin.render
Raku
In Raku, there are many approaches, but one of the simplest is to declare attributes as read/write and use thegiven keyword. The type annotations are optional, but the native gradual typing makes it much safer to write directly to public attributes.class Employee
my $employee = Employee.new;
given $employee
say $employee;
- Output:
- Name: Sally
- Surname: Ride
- Salary: 200
PHP
In PHP, one can return the current object by using the$this special variable which represent the instance. Hence return $this; will make the method return the instance. The example below defines a class Employee and three methods to set its name, surname and salary. Each return the instance of the Employee class allowing to chain methods.final class Employee
- Create a new instance of the Employee class, Tom Smith, with a salary of 100:
->setName
->setSurname
->setSalary;
- Display the value of the Employee instance:
- Display:
- Name: Tom
- Surname: Smith
- Salary: 100
Python
In Python, returningself in the instance method is one way to implement the fluent pattern.It is however discouraged by the language’s creator, Guido van Rossum, and therefore considered unpythonic for operations that do not return new values. Van Rossum provides string processing operations as example where he sees the fluent pattern appropriate.
class Poem:
def __init__ -> None:
self.title = title
def indent:
"""Indent the poem with the specified number of spaces."""
self.title = " " * spaces + self.title
return self
def suffix:
"""Suffix the poem with the author name."""
self.title = f" - "
return self
>>> Poem.indent.suffix.title
' Road Not Travelled - Robert Frost'
Swift
In Swift 3.0+ returningself in the functions is one way to implement the fluent pattern.class Person
let person = Person
.set
.set
.set