Slide 1

Slide 1 text

What I learnt using the 
 Presenter Pattern

Slide 2

Slide 2 text

Know thy pattern Primer on MVC, MVP, MVVM

Slide 3

Slide 3 text

Know thy patterns View Controller Model user events modifies fire state change events MVC

Slide 4

Slide 4 text

Know thy patterns MVP View Presenter Model User interaction user events modifies fire state change events updates

Slide 5

Slide 5 text

View Presenter Model User interaction user events modifies fire state change events updates Know thy patterns MVP Passive

Slide 6

Slide 6 text

View Model User interaction Know thy patterns MVP user events modifies updates Supervising
 Controller Data Binding

Slide 7

Slide 7 text

View Model Know thy patterns MVVM Data Binding View Model update state fire state change (view queries VM again) (Presentation Model)

Slide 8

Slide 8 text

MVVM MVC MVP View Controller Model user events modifies state change events View Presenter Model User interaction user events modifies state change events updates View Model View Model modifies state changes Know thy patterns Data Binding

Slide 9

Slide 9 text

Evolve a Pattern "Architecture changes" are painful Introducing patterns is awesome

Slide 10

Slide 10 text

Evolve a Pattern class Customer {
 String dateOfBirth; String fullName; } "Bruce Lee" "1940-11-27" Don't add to the Model! Create a ViewModel class CustomerViewModel {
 Customer user;
 
 CustomerViewModel(Customer user) {
 this.user = user;
 }
 
 boolean canPurchaseAlcohol() {
 // return age() > 18;
 }
 
 String getInitials() {
 // return strip(user.fullName);
 }
 }

Slide 11

Slide 11 text

Don't use terms interchangeably ! ViewModel != Presenter

Slide 12

Slide 12 text

Don't use terms interchangeably Model Database structure or
 mirror of REST JSON Model decorates View Model

Slide 13

Slide 13 text

Don't use terms interchangeably Activities Fragments Views decorates Presenters Model decorates View Model ! Model

Slide 14

Slide 14 text

Where to put the Presenter? ¯\_(ϑ)_/¯

Slide 15

Slide 15 text

Where to put the Presenter? Activity Fragment Fragment ListView Adapter ViewHolder ViewHolder View ActivityPresenter? FragmentPresenter? ListViewPresenter? AdapterPresenter? ViewHolderPresenter? ViewPresenter?

Slide 16

Slide 16 text

Where to put the Presenter? Activity Fragment Fragment ListView Adapter ViewHolder ViewHolder View ActivityPresenter? FragmentPresenter? ListViewPresenter? AdapterPresenter? ViewHolderPresenter? ViewPresenter?

Slide 17

Slide 17 text

Death by Boilerplate :(

Slide 18

Slide 18 text

Use some form of DataBinding! Death by Boilerplate EventBus(es) So how does one combat the Boilerplate? Use some form of DataBinding! EventBus(es)

Slide 19

Slide 19 text

CheckoutActivity CheckoutActivityPresenter CheckoutAPresenterView Interface cause Java! CheckoutAdapter PaymentCardViewHolder AdapterPresenter PaymentCardVHPresenter AdapterPresenterView VHPresenterView implements new (View activity) { mView = activity; } CheckoutActivityPresenter AdapterPresenter new (View adapter) { mView = adapter; } mCheckoutActivityPresenter mAdapterPresenter Death by Boilerplate

Slide 20

Slide 20 text

CheckoutActivity CheckoutActivityPresenter CheckoutAPresenterView CheckoutAdapter PaymentCardViewHolder AdapterPresenter PaymentCardVHPresenter AdapterPresenterView VHPresenterView new (View activity) { mView = activity; } CheckoutActivityPresenter AdapterPresenter Button.onClick(event) new (View adapter) { mView = adapter; } mCheckoutActivityPresenter mAdapterPresenter launchPaymentActivity() Death by Boilerplate

Slide 21

Slide 21 text

CheckoutActivity CheckoutActivityPresenter CheckoutAPresenterView CheckoutAdapter PaymentCardViewHolder AdapterPresenter PaymentCardVHPresenter AdapterPresenterView VHPresenterView new (View activity) { mView = activity; } CheckoutActivityPresenter AdapterPresenter Button.onClick(event) new (View adapter) { mView = adapter; } mCheckoutActivityPresenter mAdapterPresenter launchPaymentActivity() OR ...... use an EventBus Death by Boilerplate

Slide 22

Slide 22 text

CheckoutActivity CheckoutActivityPresenter CheckoutAPresenterView CheckoutAdapter PaymentCardViewHolder AdapterPresenter PaymentCardVHPresenter Death by Boilerplate (Event Bus) AdapterPresenterView VHPresenterView new (View activity) { mView = activity; } Button.onClick(event) new (View adapter) { mView = adapter; } mCheckoutActivityPresenter mAdapterPresenter launchPaymentActivity() bus.post(event) @Subscribe

Slide 23

Slide 23 text

Compose Presenters ¯\_ + _/¯

Slide 24

Slide 24 text

Compose Presenters

Slide 25

Slide 25 text

Reinforce reasons for pattern

Slide 26

Slide 26 text

(Unit) Testing is faster and easier*

Slide 27

Slide 27 text

...But UI testing exists

Slide 28

Slide 28 text

✌ fragmentedpodcast.com tech.instacart.com @kaushikgopal kaush.co My thanks to @cyrilmotier who graciously allowed me to rip-off his slide deck theme