Developing an android application by applying a software architecture pattern is always preferred by the developers. An architecture pattern gives modularity to the project files and assures that all the codes get covered in Unit testing. It makes the task easy for developers to maintain the software and to expand the features of the application in the future. MVP (Model — View — Presenter) and Model — View — ViewModel (MVVM) are the two most popular industry-recognized android architectures among developers.
The Model — View — Presenter (MVP) Pattern
MVP architecture pattern provides an easy way to structure the project codes. The reason why MVP is widely accepted is that it provides modularity, testability, and a more clean and maintainable codebase. It is composed of the following three components:
- Model: Layer for storing data. It is responsible for handling the domain logic(real-world business rules) and communication with the database and network layers.
- View: UI(User Interface) layer. It provides the visualization of the data and keep a track of the user’s action in order to notify the Presenter.
- Presenter: Fetch the data from the model and applies the UI logic to decide what to display. It manages the state of the View and takes actions according to the user’s input notification from the View.
The Model — View — ViewModel (MVVM) Pattern
MVVM pattern has some similarities with the MVP(Model — View — Presenter) design pattern as the Presenter role is played by the ViewModel. However, the drawbacks of the MVP pattern has been solved by MVVM. It suggests separating the data presentation logic(Views or UI) from the core business logic part of the application. The separate code layers of MVVM are:
- Model: This layer is responsible for the abstraction of the data sources. Model and ViewModel work together to get and save the data.
- View: The purpose of this layer is to inform the ViewModel about the user’s action. This layer observes the ViewModel and does not contain any kind of application logic.
- ViewModel: It exposes those data streams which are relevant to the View. Moreover, it servers as a link between the Model and the View.
Difference Between MVP and MVVM Design Pattern
MVP(Model View Presenter)
MVVM(Model View ViewModel)
|It resolves the problem of having a dependent View by using Presenter as a communication channel between Model and View.||This architecture pattern is more event-driven as it uses data binding and thus makes easy separation of core business logic from the View.|
|The one-to-one relationship exists between the Presenter and the View.||Multiple View can be mapped with single ViewModel.|
|The Presenter has knowledge about the View.||ViewModel has no reference to the View.|
|Model layer returns the response of the user’s input to the Presenter which forwards it to View.||After performing operations according to the user’s input, the Model layer returns the response to the View.|
|Presenter handles the application flow and the View is the actual application.||ViewModel is the actual application and View is the interface for the user in order to interact with the app.|
|The project file will contain more classes as well as code.||The Project file will contain more classes but less code per class.|
|Ideal for simple and complex applications.||Not ideal for small scale projects.|
|Easy to carry out Unit testing but a tight bond of View and Presenter can make it slightly difficult.||Unit testability is highest in this architecture.|