There are many ways to architect an Android app. But not all of them are testable and allows us to structure our code so that the app is easy to test. The key idea of a testable architecture is separating parts of the application which makes them easier to maintain, extend and test separately from each other.
In an application with a good layered architecture, this model would only be the gateway to the domain layer or business logic. See it as the provider of the data we want to display in the view.
The View, usually implemented by an Activity or Fragment, will contain a reference to the presenter. The only thing that the view will do is to call a method from the Presenter every time there is an interface action.
The Presenter is responsible to act as the middle man between View and Model. It retrieves data from the Model and returns it formatted to the View. But unlike the typical MVC, it also decides what happens when you interact with the View.
The app should be structured by package per feature. This improves readability and modularizes the app in a way that parts of it can be changed independently from each other. Each key feature of the app is in its own Java package.
Login example in the Model View Presenter (MVP) pattern
Let's see MVP in action using a simple Login Screen. There are two Buttons—one for login action and another for a registration screen; two EditTexts—one for the email and the other for the password.
LoginFragment (The View)
LoginPresenter (The Presenter)
LoginModel (The Model)
Let's see the action in the form of class diagram.
This example uses Volley for network communication, but this library is not required for MVP
UrlUtils is a class which contains all the links for my API Endpoints
ResponseErrorListener.ErrorListener is an interface that listens for error in ErrorResponse that implements Volley's Response.ErrorListener; these classes are not included here as they are not directly part of this example
Simple Login Example in MVP
Required package structure
Activity Class LoginActivity.class
Creating an ILoginView Interface
Create an ILoginView interface for update info from Presenter under view folder as follows:
Creating an ILoginPresenter Interface
Create an ILoginPresenter interface in order to communicate with LoginActivity (Views) and create the LoginPresenterCompl class for handling login functionality and reporting back to the Activity. The LoginPresenterCompl class implements the ILoginPresenter interface:
Creating a UserModel
Create a UserModel which is like a Pojo class for LoginActivity. Create an IUser interface for Pojo validations:
A Model-view-presenter (MVP) is a derivation of the model–view–controller (MVC) architectural pattern. It is used mostly for building user interfaces and offers the following benefits:
Views are more separated from Models. The Presenter is the mediator between
Model and View.
It is easier to create unit tests.
Generally, there is a one-to-one mapping between View and Presenter,
with the possibility to use multiple Presenters for complex Views.
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0