Rendra Toro - Model View Presenter

18
1 Model View Presenter Indones ia Malang, April 2016 Rendra Toro - Intel Innovator Software

Transcript of Rendra Toro - Model View Presenter

Page 1: Rendra Toro - Model View Presenter

1

Model View Presenter

Indonesia

Malang, April 2016

Rendra Toro - Intel Innovator Software

Page 2: Rendra Toro - Model View Presenter

2

why MVP

Page 3: Rendra Toro - Model View Presenter

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

Page 4: Rendra Toro - Model View Presenter

4

Model–view–presenter (MVP) is a derivation of the model–view–controller (MVC) architectural pattern, and is used mostly for building user interfaces.

Page 5: Rendra Toro - Model View Presenter

5

MVP Diagram

VIEW

PRESENTER

MODEL

User Event Updates Views

Updates Model State-Change Events

Page 6: Rendra Toro - Model View Presenter

6

Android Implementation

Page 7: Rendra Toro - Model View Presenter

7

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

Page 8: Rendra Toro - Model View Presenter

8

● Platform Specific View :

Fragment

Adapter

Activity

● Passive, limit action to model and presenter

View

Page 9: Rendra Toro - Model View Presenter

9

View Diagrams

View Interface

Android Impl

Presenter

presenter.getListing()

view.showLoading() view.showRequestError() view.generateData()

2

1

3

Presenter Interface

view.showLoading()view.showRequestError()view.generateData()

presenter.getListing()

Page 10: Rendra Toro - Model View Presenter

10

Presenter

● Business Rules happened here.

● Pure Java module without any android dependencies.

● Connection to, and from Presenter are done via interfaces

/ abstract class.

Page 11: Rendra Toro - Model View Presenter

11

Presenter Diagrams

Model

View Interface

Presenter

Validation

Workflow

Interactors

Background Thread

onSuccess(Entity entity)

onFailed(Exception e)

RepositoriesInterface

Android Impl

2

3

4Presenter Interface

1

Other Java View Impl

presenter.getListing()

view.showLoading()view.showRequestError()view.generateData()

Page 12: Rendra Toro - Model View Presenter

12

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.

Page 13: Rendra Toro - Model View Presenter

Model Diagrams

OlxService

CommunicationImpl

CommunicationInterface

APIDataSource StorageDataSource

Repositories

Storage

DataSource Interface

DataStoreFactory

PresenterRepositories

Interface1

2

3

Page 14: Rendra Toro - Model View Presenter

14

Benefits

● Each class has single responsibility,

● Each layer can be tested,

● Abstraction,

● Scalable,

● Easy to maintain.

Page 15: Rendra Toro - Model View Presenter

15

Road to Test Driven Development

● MVP

● Dependency Injection (Dagger 2)

● Unit Testing

Page 16: Rendra Toro - Model View Presenter

16

Android Libraries

● Retrofit

● Butter Knife (View Injection)

● Dagger 2 (Dependency Injection)

Android Unit Test Libraries

● View - Espresso

● Presenter - JUnit, Mockito

● Model - Robolectric, JUnit, Mockito

Page 17: Rendra Toro - Model View Presenter

17

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://github.com/konmik/konmik.github.io/wiki/Introduction-to-Model-View-Presenter-on-Android

Sample Code

https://github.com/CommunityShareCode/MVP

Page 18: Rendra Toro - Model View Presenter

18

Q & A