Kotlin Design Patterns — The Strategy

Ahmed Rizwan
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

--

--

Ahmed Rizwan
Ahmed Rizwan

Written by Ahmed Rizwan

Web | Android | Senior Software Engineer @QuixelTools

Responses (2)