Untitled

A few months ago I attended a workshop with Eric Evans, and he talked about a certain style of interface which we decided to name a fluent interface. It’s not a common style, but one we think should be better known. Probably the best way to describe it is by example.

The simplest example is probably from Eric’s timeAndMoney library. To make a time interval in the usual way we might see something like this:

— TimePoint fiveOClock, sixOClock; … — TimeInterval meetingTime = new TimeInterval(fiveOClock, sixOClock);

The timeAndMoney library user would do it this way:

— TimeInterval meetingTime = fiveOClock.until(sixOClock);

Probably the most important thing to notice about this style is that the intent is to do something along the lines of an internal DomainSpecificLanguage. Indeed this is why we chose the term ‘fluent’ to describe it, in many ways the two terms are synonyms. The API is primarily designed to be readable and to flow. The price of this fluency is more effort, both in thinking and in the API construction itself. The simple API of constructor, setter, and addition methods is much easier to write.

Martin Fowler on Fluent Interfaces: http://martinfowler.com/bliki/FluentInterface.html