MVP on Android: why do you need it?

MVP on Android: why do you need it?

30ec7b2d4dae5107b36c52fff7a29894?s=128

Stan Kocken

May 02, 2016
Tweet

Transcript

  1. MVP: why do you need it? @stan_kocken

  2. Join our team! dashlane.com/jobs Never forget another password Editor Choice

    Top Developer 4.5/5 - 1M+ downloads
  3. MVP: why do you need it?

  4. Activity

  5. BaseActivity: 1600+ lines A base activity that handles common functionality

    in the app. This includes the navigation drawer, login and authentication, Action Bar tweaks, amongst others.
  6. God Object

  7. Activity

  8. Activity/Fragments Usually contain: - actions on View - handle clicks

    - read/write in database / SharePreference - network calls - …
  9. Activity/Fragments Very hard to: - read - test - refactor

    - reuse code - …
  10. Separation of Concerns

  11. Architecture MVP Model View Presenter state-change events updates view update

    model user events
  12. Architecture MVP: in Android, my way Model View Presenter

  13. Architecture MVP: in Android, my way Model: access data store

    state View Presenter
  14. Architecture MVP: in Android, my way Model: access data store

    state View Presenter: present data to UI
  15. Architecture MVP: in Android, my way Model: access data store

    state View: touch Android View no business logic Presenter: present data to UI
  16. Architecture MVP: in Android, my way View: touch Android View

    no business logic Presenter: present data to UI Model: DataProvider access data store state
  17. Architecture MVP: in Android, my way Model: DataProvider access data

    store state View: ViewProxy touch Android View no business logic Presenter: present data to UI
  18. Presenter DataProvider ViewProxy Activity View Strong Reference Weak Reference Other

    “Strong” Reference on DataProvider, Presenter or ViewProxy forbidden. Use WeakReference. Architecture MVP: in Android, my way
  19. Example

  20. DataProvider Presenter ViewProxy Creation refreshBoard() getGameBoard() setBoxValue( int x, int

    y, int boxType);
  21. DataProvider Presenter ViewProxy User click box onSelectBox(int x, int y)

    play(int x, int y) refreshBoard() …
  22. Definition public interface BoardDef {
 
 interface IPresenter extends BaseDef.IPresenter

    {
 
 void refreshBoard();
 
 void onSelectBox(int x, int y);
 }
 
 interface IDataProvider extends BaseDef.IDataProvider {
 
 GameBoard getGameBoard();
 
 void play(int x, int y);
 }
 
 interface IView extends BaseDef.IView {
 
 void setBoxValue(int x, int y, int boxType);
 }
 }
  23. Test the UI Android stuffs TextView ImageView … View Proxy

    Already Tested by framework team
  24. Test the UI View Proxy Mock Mock Mock

  25. @stan_kocken Presentation pattern: github.com/StanKocken/Presentation TicTacToe Example: github.com/StanKocken/TicTacToe