Slide 1

Slide 1 text

MVP Pattern in Android Mgbemena Chike Software Developer | Tuts+ Instructor @chk01010 chikemgbemena.com

Slide 2

Slide 2 text

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!

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

MVC In Android – Core Architecture View Controller Model Widgets inflated from the activity_main.xml Subclass of Activity, Fragment or Service POJOs eg User

Slide 5

Slide 5 text

MVC In Android – Core Architecture User interaction View Controller Model

Slide 6

Slide 6 text

MVC In Android – Core Architecture user actions View Controller Model User interaction

Slide 7

Slide 7 text

MVC In Android – Core Architecture modifies model user actions View Controller Model User interaction

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Pros and Cons of MVC Pros • Responsibility delegation Cons • Difficulty in scaling • Lacks real modularity • Testing is a pain.

Slide 12

Slide 12 text

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.

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Introducing MVP View Presenter Model user interaction

Slide 15

Slide 15 text

Introducing MVP user actions View Presenter Model user interaction

Slide 16

Slide 16 text

Introducing MVP modifies model user actions View Presenter Model user interaction

Slide 17

Slide 17 text

Introducing MVP model changed modifies model user actions View Presenter Model user interaction

Slide 18

Slide 18 text

Introducing MVP Update view/UI model changed modifies model user actions View Presenter Model user interaction

Slide 19

Slide 19 text

Introducing MVP Keep presenters and models Android free

Slide 20

Slide 20 text

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.

Slide 21

Slide 21 text

Pros and Cons of MVP PROS: • Separation of concern • Modular • Scalable • Testable • Maintainable Cons: • Slow development speed

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

MVP - Codelab https://github.com/kolocoda/gdgdevfestsw-mvp- sample

Slide 27

Slide 27 text

Additional Resources • http://martinfowler.com/eaaDev/PassiveScreen.html • https://code.tutsplus.com/tutorials/an-introduction-to-model-view-presenter-on-android- -cms-26162 • http://fernandocejas.com/2014/09/03/architecting-android-the-clean-way/ • https://github.com/googlesamples/android-architecture • https://codelabs.developers.google.com/codelabs/android-testing/