Skip to content

Instantly share code, notes, and snippets.

@zarinfam
Created August 16, 2018 06:24
Show Gist options
  • Save zarinfam/6ddae57ee76f92f01008e73d411edea8 to your computer and use it in GitHub Desktop.
Save zarinfam/6ddae57ee76f92f01008e73d411edea8 to your computer and use it in GitHub Desktop.
How Java 8 killed the Strategy Pattern
public class Main {
@FunctionalInterface
interface Function3 <A, B, C, R> {
public R apply (A a, B b, C c);
}
public static void main(String[] args) {
BiFunction<Integer, Integer, Integer> add = Math::addExact;
BiFunction<Integer, Integer, Integer> subtract = Math::subtractExact;
BiFunction<Integer, Integer, Integer> multiply = Math::multiplyExact;
Function3<BiFunction<Integer, Integer, Integer>, Integer, Integer, Integer> execute = (callback, x, y) -> callback.apply(x, y);
System.out.println("Add: " + execute.apply(add, 3, 4));
System.out.println("Subtract: " + execute.apply(subtract, 3, 4));
System.out.println("Multiply: " + execute.apply(multiply, 3, 4));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment