Java Reference
In-Depth Information
Java Has Had Functional Programming All Along
If you ask around about Java 8 and lambdas, you may hear someone say that Java 8 makes it possible to
“do functional programming” in Java. Yet the reality is that Java has always been capable of functional
programming: it's just been disguised and awkward. What lambdas do is remove the disguise and make
it easier to work with functions directly. Although I could (and many do) try to explain this fact through
abstract arguments, this is best demonstrated through an example of practical Java code and how it changes
with the introduction of lambdas.
We will draw our example from the world of database access, since it is a place near to my heart. Before
the rise and fall of Object-Relational Mapping (ORM) frameworks 5 and the innovation of other data access
tools such as JOOQ, 6 there was the venerable JdbcTemplate from Spring's JDBC support library. 7 This
class—with help from a broad family of interfaces and supporting classes—allows developers to avoid the
coding overhead of interacting with a database, and keeping both developers and the code focused on the
relevant and interesting bits of code.
JdbcTemplate does this through a pattern called “Inversion of Control.” The basic idea is that you can
get rid of boilerplate by having some library execute the boilerplate, and then calling back to client code
with the relevant pieces of context. The “control” is “inverted” because calling into a library normally gives
control to that library, but in this case the library is giving control back to the caller. 8
A standard example of using JdbcTemplate this way (before Java 8) is given in Listing 1-11. The
boilerplate creation of the statement, execution of the statement, iteration over the result set, closing of
objects, and handling of errors is all managed by JdbcTemplate. The arguments to the JdbcTemplate call fill
in the gaps: the SQL given as the first argument is used to prepare a statement; the interface implementation
passed as the second argument is used to assign the parameters; and the interface implementation passed
as the third argument is used to consume each row of the result set.
The lambdas introduced in Java 8 make this kind of code much easier to write, much more succinct,
and much more expressive. The Java 8 example is given in Listing 1-12. Using lambdas drops the code from
16 lines to 4. How? Why did Java once require this overhead, and how does the introduction of lambdas
enable shorter code?
Listing 1-11. Example of JdbcTemplate usage before lambdas
jdbcTemplate.query("SELECT bar FROM Foo WHERE baz = ?",
new PreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps)
throws SQLException
{
ps.setString(1, "some value for baz");
}
},
new RowMapper<SomeModel>() {
@Override
public SomeModel mapRow(ResultSet rs, int rowNum)
5 E.g., Hibernate.
6
http://www.jooq.org/ .
7 API at http://bit.ly/jdbcTemplate .
8 The more generalized version of the “Inversion of Control” pattern is the “Hole in the Middle” pattern.
http://blog.enfranchisedmind.com/2007/07/the-hole-in-the-middle-pattern/ .
 
Search WWH ::




Custom Search