Add 'Syntactic Sugar' With 'Fluent Interface'

Posted on April 23, 2012

'Fluent Interface' is a Object Oriented Programming technique for adding more 'Syntactic Sugar' to the language. This just makes the code more 'Readabale' and 'Expressible'...just shows the flow of logic, in a clear way. This term (Fluent Interface) was first coined by Eric Evans and Martin Fowler. One way of achieving this, is to use 'Method Chaining'. In Java or any other Object Oriented programming language, you can achieve this by returning the Object itself (using 'this' keyword), thus allowing the calls to be chained together, in a single statement.

Below is a sample class (CustomList.class, which extends ArrayList):

import java.util.ArrayList;
* @author Babji, Chetty
public class CustomList extends ArrayList {   
    public CustomList addElem(Object e) {
        return this;

In the above class, you can see the custom 'addElem' method, which returns the Object itself (this), thus allowing it for 'Method Chaining'. Below is the 'Fluent Interface Demo' class:

import java.util.ArrayList;
* @author Babji, Chetty
public class FluentInterfaceDemo {
    public static void main(String[] args) {
        ArrayList<String> arrList = new ArrayList<String>();
        CustomList<String> custList = new CustomList<String>();

In the above class, you can see the difference between how you add elements to 'arrList' (ArrayList) and 'custList' (CustonList). That's Syntactic Sugar for you! :)

You can see this kind of implementation, in a couple of Java classes (Like StringBuffer, StringBuilder, BigDecimal, et al). Below is an example of Method Chaining, using Java's BigDecimal:

BigDecimal bigD = BigDecimal.ONE.add(BigDecimal.TEN)
                            .multiply(new BigDecimal("5"))

And below is an example of using Java's StringBuilder, which shows 'Method Chaining':

StringBuilder sb = new StringBuilder();

If you look at the Source code for StringBuilder or BigDecimal, the methods used return an Object, thus allowing it to chain. This logic can be applied to any Object Oriented Programming language. If you are familiar with JQuery Javascript Framework, you should be able to figure it out that 'Method Chaining' is extensively used in JQuery. Below is an example:

<div id="divTest1"></div>
<script type="text/javascript">
    $("#divTest1").text("Hello, world!").css("color", "blue");

Reference: JQuery Method Chaining< br/>
There are also a couple of DSL's which are specially built, using this concept of 'Fluent Interface'. Op4j is one interesting framework among 'em. Below is example code for Op4j:

Set<Calendar> set = Op.on(list).toSet().forEach()

Reference: Op4j (You can find more interesting examples in the referenced source).

Blog Categories
The views expressed on this blog are my personal views and do not reflect the views of my employer or campaigns I am supporting.

All sample code is provided for illustrative purposes only. These examples have not been thoroughly tested under all conditions. The writer therefore, cannot guarantee or imply reliability, serviceability, or function of these programs.

All programs contained herein are provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.