Jatin Sharma /
What Is the MVP Pattern?
The MVP Pattern is a software design pattern commonly used for developing Android applications. It has gained importance over the last couple of years due to the need for developing clean applications that are easy to scale, test and maintain. The MVP pattern fills this need by enabling the development of highly decoupled applications.
Why Use the MVP Pattern?
In a typical Model-View design pattern, the views and data layers of the application are tightly coupled and hence there is no layer of abstraction between the two layers. Therefore, for all practical purposes, there is only one layer handling both the views and their interaction with different data sources. This leads to redundancy in code and difficulty in scaling.
It is always a good practice to separate the data layer from the views of the app. The data can then be modeled separately in a different layer to cater to multiple views.
- Easy Data Modeling: It is always a good practice to separate the data layer from the views of the app. The data can then be modeled separately in a different layer to cater to multiple views. This is where the presentation layer comes in handy.
- Easy Debugging: By creating three different layers of abstraction it becomes easier to test and debug each layer in isolation and narrow the source of a bug down to one of the three layers.
- Increased Code Re-usability: The developer has to write a lot less code because a single data source serves all the views. This saves bandwidth since similar data doesn’t have to be queried more than once.
Layers in the MVP Pattern
Let’s dissect the architecture to look at the functions and components of each layer and how they interact with each other.
The View Layer is only responsible for handling different views visible to the user, such as activities, fragments, and dialogs.
This is the only layer that consists of different Android components and is completely orchestrated by the Presentation Layer. The View Layer delegates all the user events that occur on it to its corresponding presenter and then waits for the presenter to respond with the appropriate data.
The Presentation Layer, aka the Presenter, holds all the business logic for the application. It acts as a channel of communication between the View Layer and the Data Layer.
The Presenter too has its own interface through which both the View and Data Layers communicate.
The View Layer delegates all the events that occur on the device’s screen to the Presentation Layer. The Presenter interprets each event as a data requirement. It then fetches the required data from the Data Layer, models it, and presents it to the View Layer.
Unlike the Data Layer, the Presentation Layer is not common to the whole application and is different for each view on the mobile device. The Presenter cannot communicate with views other than its own and vice versa.
The Data Layer is responsible for all the data requirements of the application.
It consists of 4 different components:
- CRUD operations from a locally embedded database
- Data storage and retrieval from the device’s internal/external memory
- Network and API calls
- Different entity models
Each component has an interface associated with it. The interface acts as a gateway between the Presentation Layer and the Data Layer. Each interface consists of method declarations that are implemented by their respective components.
Skills AndroidQualifications :-