The Model-View-Presenter pattern is basically work on separating the presentation layer from the logic. So, when Flutter app developers read the whole coding they read all the business logic of the screen in the presentation layer.
In android app development, there are mainly three architectures available for developing an app.
1. MVC[Model-View-Controller] is most commonly useful and also a very simple architecture pattern used by many developers for developing an app.
2. MVP[Model-View-Presenter] is quite different from the MVC architecture pattern, MVP uses the presenter for dividing the code and makes it readable for the developers.
3. MVVM[Model-View-ViewModel] pattern was created to simplify the event driven programming of user interface
MVP is composed of the following components:
Model
The model Contains a data provider and the code to fetch and update the data.
View
The View, usually implemented by Activity or Fragment classes, will contain a reference to the presenter. View is updating or changing by the presenter. Presenter will call the method for updating the UI after getting the response and all interaction done using the interface action.
Presenter
The Presenter works as a middle man between View and Model. It retrieves data from the Model and passes it to the View. Also the main part is the presenter decides what happens when data passes to the View.
How to implement MVP architecture in your project:
When we implement our project the main things behind, how to create a presentation layer? Or Where we bind this layer in activity or fragment lifecycle? and also the Where we unbind the presentation layer?. These three things keep running in the mind.
So all the questions are solved when we start the implementation of the MVP architecture pattern.
First, you have to create a class for a presenter. After you have to attach this class in your activity lifecycle onCreate() method. See below sample code for how to bind your presenter from activity.
After this you have to unbind your presenter when your activity or fragment are destroyed. So you have to detach your presenter from the activity or fragment when onDestroy() lifecycle method is called.See below sample code for how to unbind your presenter from activity.
Presenters act as the middle man between View and Model. From activity or fragment you have to call any method for retrieving data from api and also fetch data from database you have to call method like below sample code.
Now see how your presenter look like,
In a presenter we create the method for calling the api of the movie list. So here you can parse your response and all formatted response pass to the view using the interface. Also you can see the onMovieResponseListner() interface. This interface we implement into the activity or fragment so we can get the response in the activity.
See below two methods we use for success and failure of the api response for updating the UI of the activity.
See we use two methods for interacting with the UI in activity. Where in one method we found the already formatted response and we only needed this response bind with the UI. Also we implement the onMovieResponseFailure() for informing users about the error.
Advantage of using MVP:
1. MVP is very understandable and easier code maintenance because it separated application's model, view and presenter in layers
2. Easy readable code
3. Much easier to test each component in isolation
Using a MVP pattern we maintain our whole application code in a one pattern and it’s also more readable to the developers. Also when you test your app so all code is already divided into different components so it's easier to do testing with separate components of your application. Basically when you follow this kind of architecture pattern for develop a app so your code is well organized.
Kotlin is simplified and universal programming language that concisely defines the simpler coding architecture. Manektech is Kotlin Android Application Development Company offering kapt and required solutions for your business needs. Putting our expertise and experience together, we aim to accomplish business objectives through tailored and modernized evelopment approach.
About Author
Subscribe to Our Newsletter!
Join us to stay updated with our latest blog updates, marketing tips, service tips, trends, news and announcements!