June 2018
Intermediate to advanced
316 pages
6h 34m
English
Higher-order functions are an extremely powerful way to reuse behavior flexibly. It's easy to implement the strategy pattern using higher-order functions.
Let's recall a good old example with compression that includes the CompressionStrategy interface:
public interface CompressionStrategy { File compress(File original);}
Here's one implementation of this interface:
public class ZipCompressionStrategy implements CompressionStrategy { @Override public File compress(File original) { throw new NotImplementedException(); }}
And the Archiver class that holds a certain strategy and archives a file:
public class Archiver { private CompressionStrategy strategy; public CompressionStrategy getStrategy() { return strategy; }
Read now
Unlock full access