How Java 8 killed the Strategy Pattern
lambda expression in Java 8, many things have changed in Java world and now Java developers can write more concise code and also use functional patterns much easier.
I read a nice article about “How Scala killed the Strategy Pattern” by Alvin Alexander that compares an old Java implementation of Strategy Pattern by Wikipedia with more concise Scala implementation, but that implementation used old Java syntax (prior Java 8) and was very verbose:
and Scala implementation of the Strategy Pattern:
It isn’t a fair comparison because after introducing
lambda experession in Java 8, we can reimplement the Strategy Pattern in Java like this:
Or even by using libraries like Vavr, we don’t need to define
Function3 functional interface and use provided one by Vavr.