Download - Model View Presenter
Model View Presenter
Indonesia
Dubai, February 2016
Rendra Toro
2
why MVP
3
● Timeline very long for one task or feature on development
to release/live
● Need to change from Manual Test to Automated Testing
● It took a lot of time try to code Automated Testing
● New method Test Driven Development
Things we faced before using MVP
4
Model–view–presenter (MVP) is a derivation of the model–view–controller (MVC) architectural pattern, and is used mostly for building user interfaces.
5
MVP Diagram
VIEW
PRESENTER
MODEL
User Event Updates Views
Updates Model State-Change Events
6
iOS implementation
7
• View: displays what it is told to by the Presenter and relays user input back to
the Presenter.
• Interactor: contains the business logic as specified by a use case.
• Presenter: contains view logic for preparing content for display (as received
from the Interactor) and for reacting to user inputs (by requesting new data
from the Interactor).
• Entity: contains basic model objects used by the Interactor.
• Routing: contains navigation logic for describing which screens are shown in
which order.
VIPER mean :
8
How they Connected
9
Android Implementation
10
Models, Net, DataStoreFactory, Repositories, Model Mapper
Interfaces
Business Rules (App Workflow, Validation)
Interfaces
View View View View View View
Detail Ad PageHome Page Search Page Chat PageMyaccount Page Other Pages
VIEW
PRESENTER
MODEL
11
● Platform Specific View (Android: Fragment, Adapter,
Activity, iOS: UIViewController, Storyboard, UITableView)
● Passive, limit action to model and presenter
View
12
View Diagrams
View Interface
Android/iOS Impl
Presenter
presenter.getListing() view.showLoading() view.showRequestError() view.generateData()
2
1
3
Presenter Interface
view.showLoading()view.showRequestError()view.generateData()
presenter.getListing()
13
Presenter
● Business Rules happened here.
● Pure Java/Obj-C module without any android/iOS
dependencies.
● Connection to, and from Presenter are done via interfaces
/ abstract class.
14
Presenter Diagrams
Model
View Interface
Presenter
Validation
Workflow
Interactors
Background Thread
onSuccess(Entity entity)
onFailed(Exception e)
RepositoriesInterface
Android/iOS Impl
2
3
4Presenter Interface
1
Other Java/Obj-c View Impl
presenter.getListing()
view.showLoading()view.showRequestError()view.generateData()
15
Model
● All data needed for the application are provided here via
Repository Implementation.
● DataStoreFactory responsible for loading data via cache
or API depends on the situation.
● Data origin is encapsulated from the client.
Model Diagrams
OlxService
CommunicationImpl
CommunicationInterface
APIDataSource StorageDataSource
Repositories
Storage
DataSource Interface
DataStoreFactory
PresenterRepositories
Interface1
2
3
17
Benefits● Each class has single responsibility,
● Each layer can be tested,
● Abstraction,
● Scalable,
● Easy to maintain.
18
Road to Test Driven Development● MVP
● Dependency Injection (Dagger 2)
● Unit Testing
19
Android Libraries
● Retrofit
● Butter Knife (View Injection)
● Dagger 2 (Dependency Injection)
Android Unit Test Libraries
● View - Espresso
● Presenter - JUnit, Mockito
● Model - Robolectric, JUnit, Mockito
20
References
http://fernandocejas.com/2014/09/03/architecting-android-the-clean-way/
https://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-architecture.html
http://antonioleiva.com/mvp-android/
https://www.objc.io/issues/13-architecture/viper/
Q & A