|
42 | 42 | By the end of this lesson, you should be able to... |
43 | 43 |
|
44 | 44 | 1. Describe: |
45 | | -- the **MVVM** design pattern |
46 | | -- the software construction problem(s) it is intended to solve |
| 45 | +- the **MVVM** architectural pattern |
| 46 | +- the problems it is intended to solve |
47 | 47 | - potential use cases for it |
48 | | -3. Assess: |
49 | | -- the suitability of a given design pattern to solve a given problem |
50 | | -- the trade offs (pros/cons) inherent in choosing high-level (MVC, MVVM, etc.) design patterns |
| 48 | +3. Assess the trade offs (pros/cons) inherent in choosing high-level (MVC, MVVM, etc.) design patterns |
51 | 49 | 4. Implement basic examples of MVVM explored in this class |
52 | 50 |
|
53 | 51 | </script></section><section data-markdown><script type="text/template"> |
54 | 52 | ### Model-View-ViewModel |
55 | 53 |
|
56 | | -**MVVM** is a __*Structural*__ design pattern which introduces a *fourth* component to MVC — *the* __*View Model.*__ |
57 | | -</script></section><section data-markdown><script type="text/template"> |
58 | | -The idea behind the MVVM pattern is that any **View** *with business logic specific to itself* should be backed by a **View Model** that represents and handles the data for the **View.** |
| 54 | +**MVVM** is a __*Structural*__ pattern which introduces a *fourth* component to MVC... |
| 55 | + |
| 56 | + |
| 57 | +*the* __*View Model.*__ |
59 | 58 | </script></section><section data-markdown><script type="text/template"> |
60 | | -In MVVM, we implement *presentational* business logic - such as converting a Date into a date-formatted String - in the __*ViewModel*__ *instead* of in the View or the Controller. |
| 59 | +The idea behind the MVVM pattern is that any **View** *with business logic specific to itself* should be backed by a **View Model** that represents and handles the data for such view. |
61 | 60 | </script></section><section data-markdown><script type="text/template"> |
62 | | - |
| 61 | +In MVVM, we implement *presentational* business logic in the __*ViewModel*__. |
| 62 | + |
| 63 | +We would do this before in the View or in the Controller. |
63 | 64 | </script></section><section data-markdown><script type="text/template"> |
64 | 65 | ### A Brief History |
65 | 66 |
|
66 | | -MVVM was invented by Microsoft architects Ken Cooper and Ted Peters specifically to simplify event-driven programming of user interfaces. |
| 67 | +MVVM was invented by Microsoft architects Ken Cooper and Ted Peters specifically to simplify **event-driven** programming of user interfaces. |
67 | 68 |
|
68 | 69 | MVVM was designed to make use of data binding functions in WPF (Windows Presentation Foundation) to better facilitate the separation of view layer development from the rest of the pattern, by removing virtually all GUI code ("code-behind") from the view layer. |
69 | 70 | </script></section><section data-markdown><script type="text/template"> |
70 | 71 | ### Components of MVVM |
71 | 72 |
|
72 | 73 | It can be seen as an extension of MVC's primary intent: separating the View from the Model and utilizing the Controller as a central "manager" between View and Model. |
73 | 74 |
|
74 | | -MVVM takes the principle of **Separation of Concerns** one step further — it adds a __*special layer*__ between the View and the Model (called the `ViewModel`) which __*allows Controllers to delegate data presentation responsibilities to the View Model.*__ |
| 75 | +MVVM takes the principle of **Separation of Concerns** one step further — it adds a __*special layer*__ between the View and the Model (called the View Model) which __*allows Controllers to delegate data presentation responsibilities to the View Model.*__ |
| 76 | +</script></section><section data-markdown><script type="text/template"> |
| 77 | + |
75 | 78 | </script></section><section data-markdown><script type="text/template"> |
76 | 79 | Here is how the major components of MVVM relate to each other (and to MVC): |
77 | 80 |
|
78 | 81 | **View** — Responsible for displaying visual elements and controls on the screen. Typically, subclasses of `UIView`. </br> |
79 | 82 | (Same as in MVC) |
80 | 83 | </script></section><section data-markdown><script type="text/template"> |
81 | | -**Model** — Objects that hold app data; typically, `structs` or simple `classes`. (Same as in MVC) |
| 84 | +**Model** — Objects that hold app data; typically, structs or simple classes. (Same as in MVC) |
82 | 85 | </script></section><section data-markdown><script type="text/template"> |
83 | 86 | **View Controller** — In MVVM, VCs still set up UIView objects, but *they do not interact with the model.* Instead, the VC uses the View Model as a *mediator* and asks for what it needs in a format that will be ready for presentation. In MVVM, Controllers should have absolutely no business logic. |
84 | 87 | </script></section><section data-markdown><script type="text/template"> |
85 | | -**View Model** — Transforms Model data into values that can be displayed on a View. Sits between the View Controller and Model. Can be a `class` or a `struct`, but it is *typically a class* so that references of the same instance can be passed around in code. |
| 88 | +**View Model** — Transforms Model data into values that can be displayed on a View. Sits between the View Controller and Model. Can be a class or a struct, but it is **typically a class** so that references of the same instance can be passed around in code. |
86 | 89 | </script></section><section data-markdown><script type="text/template"> |
87 | 90 | ### Problems Addressed |
88 | 91 |
|
89 | | -Discuss with a partner: |
| 92 | +Share in the chat: |
90 | 93 |
|
91 | 94 | - Why do you think developers turn to this architecture? |
92 | 95 |
|
93 | 96 | - What are some limitations you've noticed while using MVC? |
94 | | - |
95 | 97 | </script></section><section data-markdown><script type="text/template"> |
96 | 98 | MVVM can be used to solve two related development issues: |
97 | 99 |
|
98 | 100 | **"MVC: Massive View Controller"** — In MVC, where do you put functionality that does not belong neatly in either the View or the Model? |
99 | 101 |
|
100 | | -For expediency, iOS developers are too often tempted to use View Controllers as a "catch-all" component for such code, which results in bloated View Controllers. |
| 102 | +iOS developers are too often tempted to use View Controllers as a "catch-all" component for such code, which results in bloated View Controllers. |
101 | 103 | </script></section><section data-markdown><script type="text/template"> |
102 | 104 | __*Example*__ </br> |
103 | 105 | Data formatting is a common task. Imagine your app needs data — dates, currency, etc. — *formatted differently* for various user locales. |
|
127 | 129 |
|
128 | 130 | When the View Model knows nothing about View layout, testing each - separately or together - is much easier. |
129 | 131 | </script></section><section data-markdown><script type="text/template"> |
130 | | -## Identifying MVVM |
| 132 | +## Identifying MVVM - 20 min |
| 133 | + |
| 134 | +In pairs, check out [this project](https://github.com/amelinagzz/mvvm-notes). |
| 135 | + |
| 136 | +1. **Complete** the only TODO pending in the project. |
| 137 | + |
| 138 | +1. [Jamboard Link](https://jamboard.google.com/d/11kLepZI3XqhEYFh83bLxxSppFTI2A7NAbnt4f6oLscA/edit?usp=sharing) **Draw a diagram** to demonstrate what elements act as the Model, View Model and View. |
| 139 | + |
| 140 | +1. **Describe the responsibilities** of each component, below their names. |
131 | 141 |
|
132 | | -Individually complete [this activity](assignments/activity1.md) |
| 142 | +1. **Identify the role** played by callbacks. |
133 | 143 | </script></section><section data-markdown><script type="text/template"> |
134 | 144 | ### Benefits |
135 | 145 |
|
|
209 | 219 |
|
210 | 220 | Whenever possible, it is recommended to implement a data binding mechanism (KVO, Notifications) to automatically **handle data state changes** between the View and the View Model. |
211 | 221 | </script></section><section data-markdown><script type="text/template"> |
212 | | -## Implementing MVVM |
| 222 | +## Implementing MVVM - 25 min |
213 | 223 |
|
214 | | -In pairs complete [this activity](assignments/activity1.md) |
| 224 | +Individually complete [this activity](https://github.com/amelinagzz/mvvm-starter/tree/main) |
215 | 225 | </script></section><section data-markdown><script type="text/template"> |
216 | 226 | ## After Class |
217 | 227 |
|
|
0 commit comments