|
| 1 | +# MVVM (Model-View-ViewModel) |
| 2 | + |
| 3 | +"Model/View/ViewModel is a variation of Model/View/Controller (MVC) that is tailored for modern UI development platforms |
| 4 | +where the View is the responsibility of a designer rather than a classic developer. |
| 5 | +The designer is generally a more graphical, artistic focused person, and does less classic coding than a traditional developer.. |
| 6 | +The design is almost always done in a declarative form like HTML or XAML.." [[8]](#references) |
| 7 | + |
| 8 | + |
| 9 | +[[1]](#references) |
| 10 | + |
| 11 | +- "Model-View-ViewModel is an architectural approach used to **abstract the state and behaviour of a view**, which allows us to **separate the development of the UI from the business logic.**" [[2]](#references) |
| 12 | +- "This is accomplished by the introduction of a ViewModel, whose responsibility is to expose the data objects of a model and handle any of the application logic involved in the display of a view." [[2]](#references) |
| 13 | + |
| 14 | +## 1. Model |
| 15 | + |
| 16 | +- "Data model containing business and validation logic" [[1]](#references) [[2]](#references) |
| 17 | +- Does not contain any View reference or View related logic |
| 18 | + |
| 19 | + |
| 20 | +## 2. View |
| 21 | + |
| 22 | +- "Defines the structure, layout and appearance of a view on screen" [[2]](#references) |
| 23 | +- "View in MVVM is similar to view in PM. It contains only the UI elements. The interaction between view and ViewModel happens using Data Binding, Commands and Notifications..." [[1]](#references) |
| 24 | +- "..ViewModel only provides the data, whereas the View is responsible for consuming them." [[5]](#references) |
| 25 | +- In Android, View can consume data using _LifecycleOwner_ and _LiveData_ classes, which stops observing (unsubscribes/disposes) the data at corresponding lifecycles automatically |
| 26 | +- "Even if the View gets to decide how to handle data, that does not mean it needs to contain complicated logic. The idea is that ViewModel provides data in a form that View **simply takes and display** - no manipulation required." [[5]](#references) |
| 27 | + |
| 28 | + |
| 29 | +## 3. ViewModel |
| 30 | + |
| 31 | +- "Acts a link between the View and Model, dealing with any view logic" [[2]](#references) |
| 32 | +- "ViewModel does not hold reference to the View" [[5]](#references)[[7]](#references) |
| 33 | +- "View Model is equivalent to PresentationModel in PM pattern, it encapsulates presentation logic and data for the view. ViewModel contains the state of the view and uses Commands, DataBinding and Notifications to communicate with the view." [[1]](#references) |
| 34 | +- "Keep the ViewModel free of Android Dependencies...This allows me to utilize JUnit unit tests for a quick feedback loop" [[3]](#references)[[7]](#references) |
| 35 | +- "ViewModels hold **transient data used in the UI**, but they don’t persist data" [[6]](#references) |
| 36 | +- ViewModel class offered by Android survives configuration change (screen rotation) (using Retaining Fragment under the hood). So it can hold UI state [[4]](#references)[[5]](#references)[[7]](#references) |
| 37 | + |
| 38 | + |
| 39 | +[[4]](#references) |
| 40 | + |
| 41 | +- For one-off events (like Toast message), PublishSubject (RxJava) or SingleLiveEvent (Lifecycle Components) [[7]](#references) classes can be used. |
| 42 | +- It can be used by multiple Views |
| 43 | +- ViewModel can expose _ObservableInterface<DataState<Data>>_ to hold information about loading, success, error instead of just data. [[7]](#references) |
| 44 | + |
| 45 | + |
| 46 | +### DataBinding |
| 47 | + |
| 48 | +Pros: |
| 49 | +- Reduces boilerplate, because it binds view and logic directly without middle man |
| 50 | + |
| 51 | +Cons: |
| 52 | +- In Android, requires full package name to ViewModel class in XML |
| 53 | +- In Android, containing logic in XML is not good, it makes testing difficult. Instead, these logics can be moved to custom _BindingAdapters_ [[3]](#references) |
| 54 | +- If you want to have click listener for Android View, function in ViewModel has to return _View.OnClickListener_, which forces ViewModel to have Android code |
| 55 | + |
| 56 | + |
| 57 | +### References |
| 58 | + |
| 59 | +[1] Manoj Jaggavarapu, "Presentation Patterns : MVC, MVP, PM, MVVM," 02 05 2012. [Online]. Available: https://manojjaggavarapu.wordpress.com/2012/05/02/presentation-patterns-mvc-mvp-pm-mvvm/. [Accessed 03 03 2018]. |
| 60 | + |
| 61 | +[2] Joe Birch, "Approaching Android with MVVM" 21 09 2015. [Online]. Available: https://labs.ribot.co.uk/approaching-android-with-mvvm-8ceec02d5442. [Accessed 21 10 2020]. |
| 62 | + |
| 63 | +[3] Donn Felker, "Android MVVM with DataBinding – Removing Logic from Your Views with BindingAdapters". [Online]. Available: https://www.donnfelker.com/android-mvvm-with-databinding-removing-logic-from-your-views-with-bindingadapters/. [Accessed 21 10 2020]. |
| 64 | + |
| 65 | +[4] Luis G. Valle, "Firebase, ViewModels & LiveData" 22 10 2017. [Online]. Available: https://medium.com/@lgvalle/firebase-viewmodels-livedata-cb64c5ee4f95. [Accessed 21 10 2020]. |
| 66 | + |
| 67 | +[5] Kamil Seweryn, "MVP to MVVM transformation" 20 01 2018. [Online]. Available: https://proandroiddev.com/mvp-to-mvvm-transformation-611959d5e0ca. [Accessed 21 10 2020]. |
| 68 | + |
| 69 | +[6] Lyla Fujiwara, "ViewModels: Persistence, onSaveInstanceState(), Restoring UI State and Loaders" 17 07 2017. [Online]. Available: https://medium.com/androiddevelopers/viewmodels-persistence-onsaveinstancestate-restoring-ui-state-and-loaders-fc7cc4a6c090. [Accessed 21 10 2020]. |
| 70 | + |
| 71 | +[7] Jose Alcérreca, "ViewModels and LiveData: Patterns + AntiPatterns" 12 09 2017. [Online]. Available: https://medium.com/androiddevelopers/viewmodels-and-livedata-patterns-antipatterns-21efaef74a54. [Accessed 21 10 2020]. |
| 72 | + |
| 73 | +[8] John Gossman, "Introduction to Model/View/ViewModel pattern for building WPF apps" 08 10 2015. [Online]. Available: https://docs.microsoft.com/en-us/archive/blogs/johngossman/introduction-to-modelviewviewmodel-pattern-for-building-wpf-apps. [Accessed 21 10 2020]. |
0 commit comments