Created
December 14, 2016 00:21
-
-
Save jcdang/6529bbc0c5b753f248eb172b06e4ae48 to your computer and use it in GitHub Desktop.
This file contains 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
import java.util.function.BiFunction; | |
public interface DeathToStrategy { | |
@FunctionalInterface | |
interface Strategy extends BiFunction<Integer, Integer, Integer>{} | |
static <T> void println(T arg) { System.out.println(arg); } | |
Strategy add = (a, b) -> a + b; | |
Strategy subtract = (a, b) -> a - b; | |
Strategy multiply = (a, b) -> a * b; | |
static Integer execute(Strategy callback, Integer x, Integer y) { return callback.apply(x, y); } | |
static void main(String[] args) { | |
println("Add: " + execute(add, 3, 4)); | |
println("Subtract: " + execute(subtract, 3, 4)); | |
println("Multiply: " + execute(multiply, 3, 4)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The snippet is in response to this post: http://alvinalexander.com/scala/how-scala-killed-oop-strategy-design-pattern
Let's do it in Scala:
But wait! We didn't use the equivalent of a companion object def (static function) in the java example!