Creating Fluent Interfaces Using Lambda Expressions

We’ve been looking at the internals of methods and classes in this chapter. Now let’s shift our focus to see how lambda expressions can help shape a class’s skin or interface. We can use these techniques to structure the API of our classes, to make it more intuitive and fluent for programmers to use.

Starting with a Design

Let’s start with a simple Mailer class and evolve the design of its interface.

designing/fpij/Mailer.java
 
public​ ​class​ Mailer {
 
public​ ​void​ from(​final​ ​String​ address) { ​/*... */​ }
 
public​ ​void​ to(​final​ ​String​ address) { ​/*... */​ }
 
public​ ​void​ subject(​final​ ​String​ line) { ​/*... */​ }
 
public​ ​void​ body(​final​ ​String​ message) { ​ ...

Get Functional Programming in Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.