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

Let's talk about MVVM

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →
Avatar for JayJay JayJay
December 28, 2021

Let's talk about MVVM

Let's talk about MVVM

Avatar for JayJay

JayJay

December 28, 2021

Other Decks in Programming

Transcript

  1. Let's try to understand in a very simple manner: •

    Why do we authentically need them? ◦ Best practices. ◦ Speed up development. ◦ Programming independent. ◦ Flexible, reusable and maintainable. • How can we achieve it? ◦ e.g: MVC, MVP, MVVM, VIPER, VIP, etc.
  2. INTRODUCING MVVM VIEW VIEWMODEL MODEL • View ◦ Handles its

    own UI events. ◦ That informs the ViewModel about the user’s actions. ◦ It observes the ViewModel. • ViewModel ◦ Binds data to the View. ◦ Does not directly reference the View. ◦ It servers as a link between the Model and View. • Model ◦ Often split into layers itself: Business logic, Data access, Data retrieval. The separate code layers of MVVM are: 1 2 3 4
  3. VIEW VIEWMODEL REPOSITORY DATA SOURCE • Activities & Fragments •

    Observers the ViewModel • Keeps the UI up-to-date • Forwards user Actions back to the ViewModel
  4. VIEW VIEWMODEL REPOSITORY DATA SOURCE • Prepares & keeps the

    data for UI • Includes LiveData, Observable etc. • Survives configuration changes • The gateway for the UI Controller
  5. VIEW VIEWMODEL REPOSITORY DATA SOURCE • The complete data model

    for App. • Providers simple data modification & retrieval APIs. • Coordinates fetching, syncing, persisting etc from different data sources.
  6. VIEW VIEWMODEL REPOSITORY DATA SOURCE API to the data sources

    • e.g. Retrofit for a REST API • Room for local persistence • Content Providers
  7. Benefits • Breaks up code by responsibility. • Business logic

    is decoupled from UI. • Code reusability. • Simplifies testing. “For simple UI, M-V-VM can be overkill. In bigger cases, it can be hard to design the ViewModel up front in order to get the right amount of generality” - John Gossman Disadvantages?