Supercharged Interfaces in Kotlin

Ramesh Prasad
2 min readFeb 2, 2018

--

Kotlin Interfaces can have code which allows a new design paradigm to be used for Android App development. The idea I present here is not new and has been around in iOS world and is known as “Protocol Oriented Programming” or POP in short.

For details on Kotlin interfaces, refer to this wonderful article.

For details on POP, refer to this article.

As discussed in the article on POP, Base classes are not always suitable and every one agrees to the fact that it quickly becomes a “kitchen sink” with lots of code representing different functionality.

In the example below, I want to have two methods which will show and hide animations and are used in various Fragments in an Android App. Immediate solution that comes to mind is to have a Base Class which extends the Android Framework/Support Library Fragment class and implement the methods in it. However, I want to keep the various functionality of the Fragment seperate and neatly organized and due to the issues with Base Class discussed above, I decided to follow the POP approach.

This is how I did it-

I define and Interface with two methods having default implementations for showing and hiding animations. The animation is a Lottie component defined in the Appbar and controlled by the Activity (BaseActivity here).

Now to use these methods in a Fragment, I use the following code-

So by simply implementing the interface, I can get the show and hide animation functionality in any Fragment.

Thanks to my friends and colleagues arun  and Dan for introducing me to the concept of POP.

Please do not forget to 👏 if you find this article useful 😊

--

--