Kotlin Design Patterns — The Strategy
2 min readJun 8, 2017
Just a quick intro to the Strategy Pattern in Kotlin. And a comparison to it’s Java counterpart.
Kotlin
Java
Output
While implementing this pattern, Kotlin really shines. The reason is Higher Order functions. Functions are treated as first class citizens in the world of Kotlin. Because of this we even skipped the PrintingStrategy interface as there was no need of it.
We defined our strategy as a function that takes a String & returns a String. And we simply passed in the implementation. And… Voila!
Happy Coding!
Example taken from: Design Patterns in Kotlin
Previous Pattern: The Observer
Next Pattern: The Command