Android App Architectures: Example of MVP with Kotlin

How MVP works and its simple example showing implementation in Kotlin

Whenever any Android developer starts to think of improving his or her technical skills, the first thing that comes to mind is Architecture of the App. Not to mention that they already know the pain involved in maintaining the standard code-base after it grows.

I did the same and learned the App architectures and later focused on MVP. After trying it with test projects and later production apps, I moved to learning Kotlin language, now the latest Architecture Components. But during this transition, I realized that there are very few to none; simple examples showing the implementation of these concepts in Kotlin. So, here I am with with the examples showing the implementation of these architectural patterns in Kotlin. I promise you that these examples are gonna be so simple that you can grasp the core of this concepts with ease.

In this series we will target two main architectural patterns: Model -View-Presenter & Android Architecture Components.

1. Android App Architectures: MVP Example With Kotlin (This)
2. Android Architecture Components Example With Kotlin
3. Android Architecture Components: Advancements

Model -View-Presenter with Kotlin

While designing the architecture of any software the basic principle to keep in mind is the separation on concerns. MVP is one of the best pattern to separate presentation layer from the business logic. Moreover MVP also helps you easily implement more advanced, complete architecture patterns like Clean Architecture.

Here I am not gonna answer questions like WHAT and WHY but HOW.
How to use it ? How does it works? and How to implement it using Kotlin?
So in this 1st article we will see the implementation of MVP using Kotlin.

View

Who: Combination Activity/Fragment/View and its contract i.e. interface.
Purpose: Do all UI related stuff and all that needs Android context.
Job: Whenever it needs to do some action, it should trigger respective Presenter function using its object.

Though the XML layout is part of the view, it is not alone called as View. All necessary functions are added to our Activity/Fragment/View via contract/interface. Adapters are also the part of the View. Activity/Fragment/View initializes the Presenter and Model layer.

Example tasks:
– Show/hide progress bar.
– Request data to be shown and later show received data/error.
– Get
LayoutInflater, Resources, Android System Services.

https://medium.com/media/b08875f67b86a4c1cf97ce0329a5b602/href

Presenter

Who: It is separate class which should not have Android context.
Purpose: Acts as a mediator between View and Model.
Job: Trigger respective function of Model depending of the request made by View.

Presenter has the objects of View and Model. It should not have any business logic. Moreover it should not have direct access to any UI element.

https://medium.com/media/79f906aad7a57fe70be0ff4f136dc548/href

Model

Who: It is a separate class where all business logic will be located.
Purpose: Write all business logic here so we can test it in the isolation without View.
Job: Get/calculate data to shown on the UI.

This is where we should make API, Database calls and other calculations.
But to make it more solid, we can add one more layer inside Model:

Repository:
Decides the source of the data to be shown depending on the predefined conditions. It means, it will get the data from offline or online sources depending on the conditions like network availability.
(To keep things simple, the included app does not have repository layer.)

This way our Model will get the data to be shown irrespective of the network conditions.

https://medium.com/media/a836f093ab0ed427b21cc499207bab70/href

How does MVP works?

In brief words: View calls the function of Presenter using its object, Presenter passes it to Model, Model requests the data to be shown. After Model gets the data, it passes it to Presenter and later Presenter passes it to View where View shows that data on screen.

Here is the simple diagram of the work flow of MVP in Android. I request you to spend few minutes with the following illustration, it will help you a lot to understand MVP in Android.
Note: Model is referred as Interactor.

I hope I was clear enough to make this understandable.
Here is the complete source code of the example.

RohitSurwase/Android-Architectures-Kotlin

Thanks for reading!
Rohit Surwase

If you liked the article, clap clap clap 👏👏👏 as many times as you can.

LIKED SO MUCH! Medium allows up to 50 claps.
Also post a Tweet on Twitter.

Let’s be the friends on LinkedIn, Medium, Twitter and GitHub.


Android App Architectures: Example of MVP with Kotlin was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: