I'm sure you have already been familiar with MVC, but you also felt some pain points such as:
- Difficult to test on view and viewcontroller
- Viewcontroller are getting bigger and bigger as it handles everything
Today I'm gonna talk about another architectural pattern, MVP.
What is MVP
Model-view-presenter(MVP) is a derivation of the model-view-controller(MVC) architectural pattern, and is used mostly for building user interfaces.
— Wikipedia
The passive view part here consists of both view and viewcontroller.
- The model is the place to put your data in and defines the logic and computation to process that data.
- The view(view + viewcontroller) is a passive interface, simplely displays data, and receives user actions handling them to presenter.
- The presenter is a mediator between model and view. All presentation logic should be put in here.
Explanation with code
//Model
//Network request simulation
//Presenter
//View + Viewcontroller
There are an emptyview and a tableview in the controller. After requesting for data, It will show emptyview or filled tableview according to the request result.
Here’s the demo screenshot. You can download full demo from https://github.com/ronansparks/iOS-MVP-Demo
Advantage
- Reduce code complexity
- Easy to perform Unit test
- Viewcontrollers are not massive anymore