Fluent interface
Encyclopedia
In software engineering
, a fluent interface (as first coined by Eric Evans and Martin Fowler
) is an implementation of an object oriented
API that aims to provide for more readable code.
A fluent interface is normally implemented by using method chaining
to relay the instruction context of a subsequent call (but a fluent interface entails more than just method chaining ). Generally, the context is
This style is marginally beneficial in readability due to its ability to provide a more fluid feel to the code . However, it can be detrimental to debugging, as a fluent chain constitutes a single statement for which debuggers may not allow setting up intermediate breakpoints, for instance.
Basic Usage:
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++:
follow this practice, like Java Persistence API
:
The op4j library enables the use of fluent code for performing auxiliary tasks like structure iteration, data conversion, filtering, etc.
Also, the mock object
testing library EasyMock makes extensive use of this style of interface to provide an expressive programming interface.
This creates a lot of code and makes it difficult to see what exactly is happening here. The Packer class, visible at http://packer.dev.java.net, provides a Fluent mechanism for using this class so that you would instead write:
There are many places where Fluent APIs can greatly 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.
:
:
Basic Usage:
:
The following is a more concise code for the above example of a JavaScript
class with a fluent interface.
Software engineering
Software Engineering is the application of a systematic, disciplined, quantifiable approach to the development, operation, and maintenance of software, and the study of these approaches; that is, the application of engineering to software...
, a fluent interface (as first coined by Eric Evans and Martin Fowler
Martin Fowler
-Online presentations:* at RailsConf 2006* at JAOO 2006* at QCon London 2007 * at QCon London 2008 * at ThoughtWorks Quarterly Technology Briefing, October 2008...
) is an implementation of an object oriented
Object oriented design
Object-oriented design is the process of planning a system of interacting objects for the purpose of solving a software problem. It is one approach to software design.-Overview:...
API that aims to provide for more readable code.
A fluent interface is normally implemented by using method chaining
Method chaining
Method chaining is a common technique for invoking multiple method calls in object-oriented programming languages. Each method returns an object , allowing the calls to be chained together in a single statement...
to relay the instruction context of a subsequent call (but a fluent interface entails more than just method chaining ). Generally, the context is
- defined through the return value of a called method
- self referential, where the new context is equivalent to the last context
- terminated through the return of a void context.
This style is marginally beneficial in readability due to its ability to provide a more fluid feel to the code . However, it can be detrimental to debugging, as a fluent chain constitutes a single statement for which debuggers may not allow setting up intermediate breakpoints, for instance.
Delphi (Object Pascal)
The following example shows a class implementing a non-fluent interface, another class implementing a fluent counterpart of the aforementioned non-fluent interface, and differences in usage. The example is written in Delphi Object Pascal:Basic Usage:
C#
The following example shows a class implementing a non-fluent interface, another class implementing a fluent counterpart of the aforementioned non-fluent interface, and differences in usage. The example is written in C#:C++
A common use of the fluent interface in C++C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
is the standard iostream
Iostream
iostream is a header file which is used for input/output in the C++ programming language. It is part of the C++ standard library. The name stands for Input/Output Stream. In C++ and its predecessor, the C programming language, there is no special syntax for streaming data input or output. Instead,...
, which chains overloaded operators
Operator overloading
In object oriented computer programming, operator overloading—less commonly known as operator ad-hoc polymorphism—is a specific case of polymorphism, where different operators have different implementations depending on their arguments...
.
The following is an example of providing a fluent interface wrapper on top of a more traditional interface in C++:
Java
Some APIs in JavaJava (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
follow this practice, like Java Persistence API
Java Persistence API
The Java Persistence API, sometimes referred to as JPA, is a Java programming language framework managing relational data in applications using Java Platform, Standard Edition and Java Platform, Enterprise Edition....
:
The op4j library enables the use of fluent code for performing auxiliary tasks like structure iteration, data conversion, filtering, etc.
Also, the mock object
Mock object
In object-oriented programming, mock objects are simulated objects that mimic the behavior of real objects in controlled ways. A programmer typically creates a mock object to test the behavior of some other object, in much the same way that a car designer uses a crash test dummy to simulate the...
testing library EasyMock makes extensive use of this style of interface to provide an expressive programming interface.
Another Java Example
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.This creates a lot of code and makes it difficult to see what exactly is happening here. The Packer class, visible at http://packer.dev.java.net, provides a Fluent mechanism for using this class so that you would instead write:
There are many places where Fluent APIs can greatly 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.
PHP
The following is an example of a class with a fluent interface implemented in PHPPHP
PHP is a general-purpose server-side scripting language originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document...
:
Actionscript 3
The following is an example of a class with a fluent interface implemented in ActionscriptActionScript
ActionScript is an object-oriented language originally developed by Macromedia Inc. . It is a dialect of ECMAScript , and is used primarily for the development of websites and software targeting the Adobe Flash Player platform, used on Web pages in the form of...
:
Basic Usage:
JavaScript
The following is an example of a class with a fluent interface implemented in JavaScriptJavaScript
JavaScript is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles....
:
The following is a more concise code for the above example of a JavaScript
JavaScript
JavaScript is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles....
class with a fluent interface.