MVP Pattern in Android Using Kotlin

What is MVP?
The Model, View, Presenter pattern was based off the well known MVC pattern. It was created to alleviate the problem that often arises in the development of an android application.  In android, what often happens is that the business logic of the app is coupled with the code that handles the UI. In a lot of cases, this leads to the creation of "God objects" which greatly reduces testability and maintainability.



Basically, what MVP does is separate the UI codes and the logic, which is placed in a preferably POJO called Presenter. This effectively enables us to test our application logic without worrying about the other components. That's pretty much the gist of it. I won't dive into the topic too much, but if you want to read more about it, head over to this link.

To demonstrate this, I'll be creating a GWA(General Weighted Average) calculator app that is going to calculate my overall semester grade.

First we need to define the responsibilities of each component.
Then we define our Presenter object to contain all of the logic. Make sure to implement the contract so we know what methods we need to override.
Next, we create the View component that will handle all of the UI.
And lastly we implement the object that will handle all of the Model transactions.


And that is basically the idea of MVP. Keep in mind that this in not an architecture and is to be used with you own software architecture. If you want to see the full application code, here is the source code.

Comments

Popular Posts