I have read a nice article about "How Scala killed the Strategy Patte". In this article there is a sample Java implementation of Strategy Pattern from wikipedia but this implementation use old Java syntax (prior Java 8). I implement Scala sample using Java 8 syntax and show that 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)); } }
This implementation is more verbose than the Scala one but it is much better than the old one.
برچسب:
نویسنده: پرهام رحیمی