Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MVP(Model View Presenter) Pattern In Android

MVP(Model View Presenter) Pattern In Android

A presentation about the MVP pattern in Android for GDGDevFestSW16

Mgbemena Chike

November 25, 2016
Tweet

More Decks by Mgbemena Chike

Other Decks in Programming

Transcript

  1. Agenda • MVC in Android • Pros and Cons of

    MVC in Android • Introducing MVP in Android • Difference between MVC and MVP in Android. • Pros and cons of MVP in Android • Show us code Jare!
  2. MVC In Android An architectural pattern is a general, reusable

    solution to a commonly occurring problem in software architecture within a given context. It’s the only way to maintain a project clean, expansible and testable. Model–view–controller (MVC) is a software architectural pattern mostly (but not exclusively) for implementing user interfaces oncomputers. It divides a given software application into three interconnected parts, so as to separate internal representations of information from the ways that information is presented to or accepted from the user. - Wikipedia
  3. MVC In Android – Core Architecture View Controller Model Widgets

    inflated from the activity_main.xml Subclass of Activity, Fragment or Service POJOs eg User
  4. MVC In Android – Core Architecture model changed Model changed

    modifies model user actions View Controller Model User interaction
  5. MVC In Android – Core Architecture update view model changed

    Model changed modifies model user actions View Controller Model User interaction
  6. MVC In Android – Core Architecture update view model changed

    Model changed modifies model user actions View Controller Model User interaction
  7. Pros and Cons of MVC Pros • Responsibility delegation Cons

    • Difficulty in scaling • Lacks real modularity • Testing is a pain.
  8. Introducing MVP (M)Model (P)Presenter (V)View The interface defining data to

    be displayed Acts upon the model and the view. A mediator. A passive interface that displays, routes user actions to the presenter to act upon.
  9. Introducing MVP (V)View A passive interface that displays, routes user

    actions to the presenter to act upon. A Passive View handles this by reducing the behavior of the UI components to the absolute minimum by using a controller that not just handles responses to user events, but also does all the updating of the view. This allows testing to be focused on the controller with little risk of problems in the view. – Martin Fowler The Passive View
  10. Difference between MVC and MVP In MVP, the view is

    only given information by the presenter but in MVC both the view and model can interact with themselves The MVP also introduces the Passive View Pattern by Martin Flowler.
  11. Pros and Cons of MVP PROS: • Separation of concern

    • Modular • Scalable • Testable • Maintainable Cons: • Slow development speed
  12. MVP – Separation of Concerns Each layers: View layer, Presentation

    layer and model layer are bound by a contract Model and View layer never interact with each other View layer is passive Android APIs resides in the View Layer Model & Presenter are pure Java code Each layer have clearly defined functionalities by the use of a contract, this makes it easier to test, Find and fix bugs
  13. MVP - Modularity You create packages for screen e.g. if

    you have a user detail screen, you create a package called userdetail Every screen is in its own package containing the Activity/Fragment (View), Presenter & Contract The advantage of this is that it is very easy to modify or add new features with minimal collision
  14. MVP - Scalability Access to the Model Layer is given

    in data repository format Model layer is not bound to any contract hence making it accessible to all parts of the app With all parts of the app placed under specific boundaries, modifications or new updates pose minimal impact on existing pieces
  15. MVP - Testing Easier to write tests as the complete

    app is completely decoupled. Our models and presenters are pure Java, they can be easily tested using JUnit High increase in testing speed since Junit are faster than Espressor and other Android Tests