Deep Dive into MVP(0x01)
MVP
MVP(Model-View-Presenter) pattern is a derivative from the well known MVC
(Model-View-Controller) which for a while now is gaining importance in the development of Android applications.
What does MVP really means?
The MVP allows seprate the presentation layer from the logic, so that the things about how the interface works are separate from how we represent it on screen.
Keep in mind: MVP is not an architecture pattern, it’s only responsible for the presentation layer.
How to use MVP?
As it’s talked above, MVP is just a design pattern to decoupling the presentation layer. So there are a lot of ways to implement it. In this topic, we will show one implementation by combining the real project.
So let’s start!!
Implementation
Consider about below diagram:
sequenceDiagram
View->>Presenter: doLogin
Presenter->>Model: goLogin!
Model->>Presenter: login successfully
Presenter->>View: Show successful message
View is not talking to Model anymore, instead, Presenter is the mid-layer to communicate things with each other.
Further, all of these layers should be platform independent especially on View. Most of time, we are using Activity/Fragment to display screen UI on android platform as View layer. If the Presenter calls Activity directly, it has too strong reference to the special platform, and its hard to make our goal: presenter layer is seprated from the logic of how the UI interface works. By instead, we should define an abstract interface for View layer so that it doesn’t care about if it’s implemented by Activity or Fragment or anything else.
View
Presenter
ViewDelegate
Now we have general framework setup with MVP pattern!