What I learnt using the Presenter Pattern

What I learnt using the Presenter Pattern

Given at Oredev 2016: https://vimeo.com/191068122

Patterns of yore like MVP, MVVM etc. have taken the Android world by storm these days. These are not new concepts per say, but their application to the world of mobile development is new.

In this talk, I share some of the learnings from having adopted this pattern in an actual production application.

What are the common patterns used in mobile these days? What are the problems with adoption? What are the advantages of adoption? How do we adopt these patterns in a way that doesn't lead to death by abstraction?

We'll discuss the juicy learnings from war stories in this session.

A487b8723907637cb1af973bc5957bb4?s=128

Kaushik Gopal

November 10, 2016
Tweet

Transcript

  1. 4.

    Know thy patterns MVP View Presenter Model User interaction user

    events modifies fire state change events updates
  2. 5.

    View Presenter Model User interaction user events modifies fire state

    change events updates Know thy patterns MVP Passive
  3. 6.

    View Model User interaction Know thy patterns MVP user events

    modifies updates Supervising
 Controller Data Binding
  4. 7.

    View Model Know thy patterns MVVM Data Binding View Model

    update state fire state change (view queries VM again) (Presentation Model)
  5. 8.

    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
  6. 10.

    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);
 }
 }
  7. 15.

    Where to put the Presenter? Activity Fragment Fragment ListView Adapter

    ViewHolder ViewHolder View ActivityPresenter? FragmentPresenter? ListViewPresenter? AdapterPresenter? ViewHolderPresenter? ViewPresenter?
  8. 16.

    Where to put the Presenter? Activity Fragment Fragment ListView Adapter

    ViewHolder ViewHolder View ActivityPresenter? FragmentPresenter? ListViewPresenter? AdapterPresenter? ViewHolderPresenter? ViewPresenter?
  9. 18.

    Use some form of DataBinding! Death by Boilerplate EventBus(es) So

    how does one combat the Boilerplate? Use some form of DataBinding! EventBus(es)
  10. 19.

    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
  11. 20.

    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
  12. 21.

    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
  13. 22.

    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