Separating Concerns Using Lambda Expressions

We often create classes to reuse code; we have good intentions, but it’s not always the right choice. By using higher-order functions, we can accomplish the same goals without needing a hierarchy of classes.

Exploring Design Concerns

Let’s start with an example to sum asset values as a way to illustrate the design idea of separation of concerns. We’ll build it in iterations. The design we first create will mix multiple concerns in one method, but we’ll quickly refactor to make the method more cohesive. Let’s start with an Asset class.

designing/fpij/Asset.java
 
public​ ​class​ Asset {
 
public​ ​enum​ AssetType { BOND, STOCK };
 
private​ ​final​ AssetType type;
 
private​ ​final​ ​int​ value; ...

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.