O'Reilly logo

Functional Programming in Java by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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) { ​ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required