Android Application Architecture Flashcards
What is MVP and how to different from MVC ?
The Model View Presenter (MVP) architecture pattern improve the application architecture to increase testability. The MVP pattern separates the data model, from a view through a presenter.
- ## The viewA view component in MVP contains a visual part of the application.
It contains only the UI and it does not contain any logic or knowledge of the data displayed.
In typical implementations the view components in MVP exports an interface that is used by the Presenter.
The presenter uses these interface methods to manipulate the view. Example method names would be: showProgressBar, updateData.
- ## The presenterThe presenter triggers the business logic and tells the view when to update.
It therefore interacts with the model and fetches and transforms data from the model to update the view. The presenter should not have, if possible, a dependency to the Android SDK.
- ## The modelContains a data provider and the code to fetch and update the data.
This part of MVP updates the database or communicate with a webserver.
MVP makes it easier to test your presenter logic and to replace dependencies. But using MVP also comes with a costs, it makes your application code longer. Also as the standard Android templates at the moment do not use this approach, not every Android developer will find this code structure easy to understand.
In the Model View Presenter pattern, the views more separated from the model. The presenter communicates between model and view. This makes it easier to create unit tests Generally there is a one to one mapping between view and Presenter, but it is also possible to use multiple presenters for complex views.
In the Model View Controller pattern, the controllers are behavior based and can share multiple views. View can communicate directly with the model.
MVP is currently on of the patterns that the Android community prefers.
What is MVVM and how to different from MVP ?
The Model View View Model design pattern is also known as Model View Binder.
- The view
- —————–
A view component in MVP contains a visual part of the application.
The view binds to observable variables and actions exposed by the view model typically using the data binding framework.
The view is responsible for handling for example:
Menus Permissions Event listeners Showing dialogs, Toasts, Snackbars Working with Android View and Widget Starting activities All functionality which is related to the Android Context
- The view model
- ————————————–
The view model contains the data required for the view.
It is an abstraction of the view and exposes public properties and commands.
It uses observable data to notify the view about changes.
It also allows to pass events to the model.
It is also a value converter from the raw model data to presentation-friendly properties.
The view model has the following responsibilities:
Exposing data Exposing state (progress, offline, empty, error, etc) Handling visibility Input validation Executing calls to the model Executing methods in the view
The view model should only know about the application context. the application context can:
Start a service Bind to a service Send a broadcast Register a broadcast receiver Load resource values It cannot: Show a dialog Start an activity Inflate a layout
- The model
- ———————–
Contains a data provider and the code to fetch and update the data. The data can be retrieved from different sources, for example:
REST API Realm db SQLite db Handles broadcast Shared Preferences Firebase etc.
Basically the same as the model in the MVP.
- MVVM uses data binding and is therefore a more event driven architecture.
- MVP typically has a one to one mapping between the presenter and the view, while MVVM can map many views to one view model.
- In MVVM the view model has no reference to the view, while in MVP the view knows the presenter.