Created
August 16, 2018 06:24
-
-
Save zarinfam/6ddae57ee76f92f01008e73d411edea8 to your computer and use it in GitHub Desktop.
How Java 8 killed the Strategy Pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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