Model View Presenter (MVP) Architecture on Android
Model-View-Presenter (MVP) is a derivation of the Model-View-Controller (MVC) architecture, which is a common architectural pattern in android development. As its name expressed, it contains three sections, namely Model, View, and Presenter.
Definitions
Model
If you have experience working with code architecture, undoubtedly you heard Model. The model is the central component of a pattern, which is the gateway to the domain layer or business logic. In other words, the duty of the model is to bring data to the application from API, database, and so on. The duty of a model is providing data from:
- API
- Database
- Preferences
- etc.
View
A part of code which has the duty to visualize the data (UI). The View is part of an android app that handles Activities, Fragments, and other Layouts. The duty of a view is handling:
- activities
- fragments
- layouts
- events
- permissions
- etc.
Presenter
The presenter is a mediator between the model and the view, which handles view updates based on model changes. It contains the business logic of an application, which something similar to the Controller in Model-View-Controller.
Contract
The contract is an interface that specifies the contract between each view and presenter.
Why Model-View-Presenter (MVP)?
Activities in Android handle both data and user interface. It is difficult to divide the model, view, and logic from each other. MVP is a way that can divide view and model, by adding a presenter as a mediator. It is easier to create a test unit because all the logics are in presenter
How Model-View-Presenter (MVP) works
For knowing about how MVP works, it is better to know some key points about MVP:
- All the logic should be in presenters
- Presenters should be Android-free
- Presenters provide data for views
- Views just responsible for how to display the data
There are two different flows in MVP architecture:
- data changes (Model) -> presenter notified (Presenter) -> UI changes (View)
- Events in UI (View) -> Invoke a method in presenter (Presenter) -> update model (Model)
How to implement Model-View-Presenter (MVP)
Here for implementation, we consider that there is just only one host Activity, which is MainActivity, and the navigations are through fragments.
For each page (fragment) inside the app, it should be created two classes and one interface. For example, for the login page inside the app, there are:
- LoginFragment, which handles the user interface of the login page
- LoginPresenter, which contains all the logics
- LoginContract, which is an interface specifies the contract between LoginFragment and LoginPresenter
It should be mention that the following steps are not the only approach for implementing MVP.
1. Base Contract
First, we should create a base contract:
2. Base Presenter
An abstract class for the base presenter:
3. Login Contract
Login contract’s members implement BaseContract members such as below:
4. Login Fragment
Login fragment implements LoginContract.View:
5. LoginPresenter
The presenter for login fragment should extend BasePresenter and implement LoginContract.Presenter:
Some Points
- Presenters should be Android-free
- There is a one to one mapping between View and Presenter
- View just handles updating the UI and events
- Models in MVP are similar to MVC
Conclusion
Due to the structure of android development, it is a bit difficult to divide the view and the model. Model-View-Presenter is an architectural pattern that can help to achieve this. It helps to put all the business logic inside the presenters, so activities and fragments just handling the visualization. It is why MVP is a unit test friendly.