June 2018
Intermediate to advanced
280 pages
7h 46m
English
The decorator can be implemented by leveraging the function composition. For example, adding logging to an existing function call can be done, as shown earlier, by using the stream peek method and log to the console from the provided peek Consumer<T>.
Our Chapter 4, Structural Patterns, decorator example can be rewritten in functional style; notice that the decorator is used to consume the same input as the initial decorated consumer:
jshell> Consumer<String> toASCII = x -> System.out.println("Print ASCII: " + x);toASCII ==> $Lambda$159/1690859824@400cff1ajshell> Function<String, String> toHex = x -> x.chars().boxed().map(y -> "0x" + Integer.toHexString(y)).collect(Collectors.joining(" "));toHex ==> $Lambda$158/1860250540@55040f2f ...Read now
Unlock full access