Download Fluent

0 views
Skip to first unread message

Keri Gamrath

unread,
Aug 5, 2024, 9:41:02 AM8/5/24
to upwivasu
Afew months ago I attended a workshop with Eric Evans, and hetalked about a certain style of interface which we decided to name afluent interface. It's not a common style, but one we think shouldbe better known. Probably the best way to describe it is by example.

I'll continue with the common example of making out an order for acustomer. The order has line-items, with quantities and products. Aline item can be skippable, meaning I'd prefer to deliver withoutthis line item rather than delay the whole order. I can give thewhole order a rush status.


In essence we create the various objects and wire them uptogether. If we can't set up everything in the constructor, then weneed to make temporary variables to help us complete the wiring -this is particularly the case where you're adding items into collections.


Probably the most important thing to notice about this style isthat the intent is to do something along the lines of an internalDomainSpecificLanguage. Indeed this is why we chose theterm '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 ofthis fluency is more effort, both in thinking and in the APIconstruction itself. The simple API of constructor, setter, andaddition methods is much easier to write. Coming up with a nice fluentAPI requires a good bit of thought.


Indeed one of the problems of this little example is that I justknocked it up in a Calgary coffee shop over breakfast. Good fluentAPIs take a while to build. If you want a much more thought outexample of a fluent API take a look at JMock. JMock, like any mockinglibrary, needs to create complex specifications of behavior. Therehave been many mocking libraries built over the last few years,JMock's contains a very nice fluent API which flows verynicely. Here's an example expectation:


So far we've mostly seen fluent APIs to create configurations ofobjects, often involving value objects. I'm not sure if this is adefining characteristic, although I suspect there is something aboutthem appearing in a declarative context. The key test of fluency,for us, is the Domain Specific Language quality. The more the use ofthe API has that language like flow, the more fluent it is.


Building a fluent API like this leads to some unusual API habits.One of the most obvious ones are setters that return a value. (In theorder example with adds an order line to the order and returns theorder.) The common convention in the curly brace world is thatmodifier methods are void, which I like because it follows theprinciple of CommandQuerySeparation. This convention doesget in the way of a fluent interface, so I'm inclined to suspend theconvention for this case.


You should choose your return type based on what you need tocontinue fluent action. JMock makes a big point of moving its typesdepending on what's likely to be needed next. One of the nice benefitsof this style is that method completion (intellisense) helps tell youwhat to type next - rather like a wizard in the IDE. In general I finddynamic languages work better for DSLs since they tend to have a lesscluttered syntax. Using method completion, however, is a plus forstatic languages.


One of the problems of methods in a fluent interface is that theydon't make much sense on their own. Looking at a method browser ofmethod by method documentation doesn't show much sense towith. Indeed sitting there on its own I'd argue thatit's a badly named method that doesn't communicate its intent at allwell. It's only in the context of the fluent action that it showsits strengths. One way around this may be to use builder objectsthat are only used in this context.


One thing that Eric mentioned was that so far he's used, andseen, fluent interfaces mostly around configurations of value objects.Value objects don't have domain-meaningful identity so you can makethem and throw them away easily. So the fluency rides on making newvalues out of old values. In that sense the order example isn't thattypical since it's an entity in the EvansClassification.


I haven't seen a lot of fluent interfaces out there yet, so Iconclude that we don't know much about their strengths andweaknesses. So any exhortations to use them can only be preliminary -however I do think they are ripe for more experimentation.


Update (23 June 2008). Since I wrote this post this term's beenused rather widely, which gives me a nice feeling of tinglygratification. I've refined my ideas about fluent interfaces andinternal DSLs in the book I've been working on. I've also noticed acommon misconception - many people seem to equate fluent interfaceswith Method Chaining. Certainly chaining is a common technique touse with fluent interfaces, but true fluency is much more than that.


Boost your confidence and learn how to have difficult conversations at work by role-playing real-world examples and acquiring effective research-backed tools to develop equitable language and an inclusive workplace culture.


To successfully innovate and maintain a competitive advantage, leaders of organizations must understand how to operationalize inclusion throughout their business practices and become an inclusive leader. This requires a new leadership profile: Equity Fluent Leadership.


The Equity Fluent Leadership (EFL) Academy, an executive leadership training, was created to provide leaders with the inclusive leadership skills and tools they need to become equity-fluent and cultivate a more inclusive culture at work.


Throughout this profoundly transformative leadership training program and equity and inclusion training, participants will learn the tools and strategies to foster belonging and psychological safety in their organization and teams, how to operationalize diversity, equity, and inclusion, inclusive leadership, and how to have courageous conversations.


We know that time away from the office is precious, so we work hard to make our program experiences as productive and valuable as possible. Our programs span full business days starting between 8:30-9:00 a.m. and running through 5 p.m. with lunch served in the classroom and short breaks throughout the day.


Our programs are non-degree programs and do not hold any credit unit equivalency, letter grades, pass/fail designation, Continuing Education Units (CEUs), or transcripts. Rather, we provide a certificate of completion at the conclusion of the program.


Yes. Enrollment is available to global leaders, but visa requirements may apply. Proficiency in written and spoken English at a level that allows full engagement and participation in a program is recommended.


The list of countries sanctioned by the Treasury Department's Office of Foreign Asset Control currently includes: Cuba, Iran, North Korea, Syria and the Ukraine-Crimea Region, plus the addition of Russia for Berkeley Executive Education at this time.


Signal your professional achievement to your network and get recognized for your completion! Upon successful completion of the program, UC Berkeley Executive Education grants a verified digital certificate of completion to participants that you are encouraged to add to your profiles.


Note: This program results in a digital certificate of completion and is not eligible for degree credit/CEUs. After successful completion of the program, your verified digital certificate will be emailed to you directly. All certificate images are for illustrative purposes only and may be subject to change at the discretion of UC Berkeley Executive Education.


Participants in the Certificate of Business Excellence (COBE) program will earn a mark of distinction from a world-class university, gain access to a powerful global network, and enjoy the flexibility of completing the program in up to three years. A UC Berkeley Certificate of Business Excellence allows individuals to create a personal learning journey structured by our four academic pillars to gain management essentials in Leadership, Entrepreneurship, Strategy, and Finance to drive both personal and organizational development.


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 (DSL). The term was coined in 2005 by Eric Evans and Martin Fowler.[1]


A fluent interface is normally implemented by using method chaining to implement method cascading (in languages that do not natively support cascading), concretely by having each method return the object to which it is attached, often referred to as this or self. Stated more abstractly, a fluent interface relays the instruction context of a subsequent call in method chaining, where generally the context is


Note that a "fluent interface" means more than just method cascading via chaining; it entails designing an interface that reads like a DSL, using other techniques like "nested functions and object scoping".[1]


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 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 (from 1988 in Lisp) and the Amulet system (from 1994 in C++) which used this style for object creation and property assignment.


Fluent interface can also be used to chain a set of method, which operates/shares the same object. Instead of creating a customer class, we can create a data context which can be decorated with fluent interface as follows.


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.

3a8082e126
Reply all
Reply to author
Forward
0 new messages