Quantcast
Channel: Jarrod Roberson: Programming Missives » api
Viewing all articles
Browse latest Browse all 2

Enforcing Fluent API adherence using Interfaces in Java

0
0

This some considerable noise on the internet about Fluent APIs and using them. There was a question on stackoverflow.com about how to enforce calling all the required methods to guarantee you fully materialize an object when using a Builder Pattern with a Fluent API approach.

The appropriate way to achieve this is through Interfaces that represent each of the steps in the procedure. You have to make the constructor to resulting object that implements these interfaces private so that no one can directly create the object, and you probably want to make the class private as well so no one can cast directly to the resulting object from a reference to one of the intermediate Interfaces.

Here is the code for one example on how to have a Fluent API that enforces the order in which the methods are called and protects the object being built.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images