Skip to content

Commit 8541f03

Browse files
committed
MVVM notes
1 parent 64da185 commit 8541f03

10 files changed

+81
-46
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
21
# Created by https://www.gitignore.io/api/linux,macos,windows
2+
.idea/
33

44
### Linux ###
55
*~

README.md

+4-9
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,13 @@ General and Android specific
1010
## Presentation Patterns (Delivery Mechanisms)
1111
### 1. [MVC notes](https://github.com/jemshit/android_architecture_notes/blob/master/mvc_cheatsheet.md)
1212

13-
This is not Android specific note, but MVC variants in general. So difference between these patterns can be seen.
13+
This is MVC variants in general (not Android specific), so difference between these patterns can be seen.
1414

1515
### 2. [MVP notes](https://github.com/jemshit/android_architecture_notes/blob/master/mvp_cheatsheet.md)
16-
Especially from Android perspective
16+
MVP in general and from Android perspective
1717

18-
### 3. MVVM notes
19-
Not ready yet
18+
### 3. [MVVM notes](https://github.com/jemshit/android_architecture_notes/blob/master/mvvm_cheatsheet.md)
19+
MVVM in general and from Android perspective
2020

2121
### 4. [Pattern Differences note](https://github.com/jemshit/android_architecture_notes/blob/master/pattern_differences.md)
2222
MVC vs MVP vs MVVM
23-
24-
---
25-
- To contribute, add comment to these issues [clean architecture issue](https://github.com/jemshit/android_architecture_notes/issues/1), [mvp issue](https://github.com/jemshit/android_architecture_notes/issues/2), [mvvm issue](https://github.com/jemshit/android_architecture_notes/issues/3)
26-
27-
- To discuss, create new issue.

clean_architecture_cheatsheet.md

-3
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,3 @@ As you can see, it makes sense to separate "Infrastructure" (as in Onion archite
332332
[11] Martin Fowler, "InversionOfControl," 26 06 2005. [Online]. Available: https://martinfowler.com/bliki/InversionOfControl.html. [Accessed 02 03 2018]
333333

334334
[12] Martin Fowler, "Registry," [Online]. Available: https://martinfowler.com/eaaCatalog/registry.html. [Accessed 02 03 2018]
335-
336-
---
337-
Last Edited: 05.03.2018

media_files/mvvm.png

1.58 KB
Loading

media_files/mvvm_viewmodel_scope.png

23.9 KB
Loading

mvc_cheatsheet.md

-3
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,3 @@ Figure reference: [[6]](#4-references)
126126
[5] Stephen Walther, "The Evolution of MVC," 24 08 2008. [Online]. Available: http://stephenwalther.com/archive/2008/08/24/the-evolution-of-mvc. [Accessed 02 03 2018].
127127

128128
[6] 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].
129-
130-
---
131-
Last Edited: 04.03.2018

mvp_cheatsheet.md

-4
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,3 @@ https://overflow.buffer.com/2017/12/21/even-map-though-data-model-mapping-androi
314314
[19] "What are MVP and MVC and what is the difference?," [Online]. Available: https://stackoverflow.com/a/101561/3736955. [Accessed 03 03 2018].
315315

316316
[20] Jose Alcérreca, "ViewModels and LiveData: Patterns + AntiPatterns," [Online]. Available: https://medium.com/@JoseAlcerreca/yep-if-you-use-viewmodel-as-a-presenter-you-need-a-reference-to-the-view-b853879ccd0e. [Accessed 21 03 2018].
317-
318-
319-
---
320-
Last Edited: 04.03.2018

mvvm_cheatsheet.md

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
![MVVM](https://raw.githubusercontent.com/jemshit/android_architecture_notes/master/media_files/mvvm.png)
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+
![MVVM](https://raw.githubusercontent.com/jemshit/android_architecture_notes/master/media_files/mvvm_viewmodel_scope.png)
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].

mvvm_cheatsheet_draft.md

-24
This file was deleted.

pattern_differences.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ that the loading animation should be displayed now or that the ListView should b
7373

7474
- You’ll also notice that the View has no idea about the Model in the MVVM pattern. This is because, as far as the View knows, its “Model” IS the View Model (hence its name). Because of how data-binding and other features like commanding work in WPF and Silverlight, there is rich communication between the View and View Model, isolating the View from having to know anything about what’s really happening behind the scenes." [[1]](#3-references)
7575

76+
- "..ViewModel only provides the data, whereas the View is responsible for consuming them." [[7]](#references)
77+
7678
#### d) PM
7779
Presentation Model definition: https://martinfowler.com/eaaDev/PresentationModel.html
7880

@@ -90,5 +92,4 @@ Presentation Model definition: https://martinfowler.com/eaaDev/PresentationModel
9092

9193
[6] Martin Fowler, "GUI Architectures," 18 07 2006. [Online]. Available: https://martinfowler.com/eaaDev/uiArchs.html. [Accessed 26 02 2018].
9294

93-
---
94-
Last Edited: 04.03.2018
95+
[7] Kamil Seweryn, "MVP to MVVM transformation" 20 01 2018. [Online]. Available: https://proandroiddev.com/mvp-to-mvvm-transformation-611959d5e0ca. [Accessed 21 10 2020].

0 commit comments

Comments
 (0)