Skip to content

Commit f7bef02

Browse files
committed
Edit L07
1 parent 3dbda3a commit f7bef02

File tree

2 files changed

+62
-42
lines changed

2 files changed

+62
-42
lines changed

Lessons/07-MVVM/README.md

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,38 +25,37 @@
2525
By the end of this lesson, you should be able to...
2626

2727
1. Describe:
28-
- the **MVVM** design pattern
29-
- the software construction problem(s) it is intended to solve
28+
- the **MVVM** architectural pattern
29+
- the problems it is intended to solve
3030
- potential use cases for it
31-
3. Assess:
32-
- the suitability of a given design pattern to solve a given problem
33-
- the trade offs (pros/cons) inherent in choosing high-level (MVC, MVVM, etc.) design patterns
31+
3. Assess the trade offs (pros/cons) inherent in choosing high-level (MVC, MVVM, etc.) design patterns
3432
4. Implement basic examples of MVVM explored in this class
3533

3634

3735
<!-- > -->
3836

3937
### Model-View-ViewModel
4038

41-
**MVVM** is a __*Structural*__ design pattern which introduces a *fourth* component to MVC*the* __*View Model.*__
39+
**MVVM** is a __*Structural*__ pattern which introduces a *fourth* component to MVC...
4240

43-
<!-- > -->
4441

45-
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.**
42+
*the* __*View Model.*__
4643

4744
<!-- > -->
4845

49-
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.
46+
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.
5047

5148
<!-- > -->
5249

53-
![mvvm](mvvm.png)
50+
In MVVM, we implement *presentational* business logic in the __*ViewModel*__.
51+
52+
We would do this before in the View or in the Controller.
5453

5554
<!-- > -->
5655

5756
### A Brief History
5857

59-
MVVM was invented by Microsoft architects Ken Cooper and Ted Peters specifically to simplify event-driven programming of user interfaces.
58+
MVVM was invented by Microsoft architects Ken Cooper and Ted Peters specifically to simplify **event-driven** programming of user interfaces.
6059

6160
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.
6261

@@ -66,7 +65,11 @@ MVVM was designed to make use of data binding functions in WPF (Windows Presenta
6665

6766
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.
6867

69-
MVVM takes the principle of **Separation of Concerns** one step further &mdash; 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.*__
68+
MVVM takes the principle of **Separation of Concerns** one step further &mdash; 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.*__
69+
70+
<!-- > -->
71+
72+
![mvvm](mvvm.png)
7073

7174
<!-- > -->
7275

@@ -77,34 +80,33 @@ Here is how the major components of MVVM relate to each other (and to MVC):
7780

7881
<!-- > -->
7982

80-
**Model** &mdash; Objects that hold app data; typically, `structs` or simple `classes`. (Same as in MVC)
83+
**Model** &mdash; Objects that hold app data; typically, structs or simple classes. (Same as in MVC)
8184

8285
<!-- > -->
8386

8487
**View Controller** &mdash; 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.
8588

8689
<!-- > -->
8790

88-
**View Model** &mdash; 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.
91+
**View Model** &mdash; 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.
8992

9093
<!-- > -->
9194

9295
### Problems Addressed
9396

94-
Discuss with a partner:
97+
Share in the chat:
9598

9699
- Why do you think developers turn to this architecture?
97100

98101
- What are some limitations you've noticed while using MVC?
99102

100-
101103
<!-- > -->
102104

103105
MVVM can be used to solve two related development issues:
104106

105107
**"MVC: Massive View Controller"** &mdash; In MVC, where do you put functionality that does not belong neatly in either the View or the Model?
106108

107-
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.
109+
iOS developers are too often tempted to use View Controllers as a "catch-all" component for such code, which results in bloated View Controllers.
108110

109111
<!-- > -->
110112

@@ -146,9 +148,17 @@ When the View Model knows nothing about View layout, testing each - separately o
146148

147149
<!-- > -->
148150

149-
## Identifying MVVM
151+
## Identifying MVVM - 20 min
152+
153+
In pairs, check out [this project](https://github.com/amelinagzz/mvvm-notes).
154+
155+
1. **Complete** the only TODO pending in the project.
156+
157+
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.
158+
159+
1. **Describe the responsibilities** of each component, below their names.
150160

151-
Individually complete [this activity](assignments/activity1.md)
161+
1. **Identify the role** played by callbacks.
152162

153163
<!-- > -->
154164

@@ -250,9 +260,9 @@ Whenever possible, it is recommended to implement a data binding mechanism (KVO,
250260

251261
<!-- > -->
252262

253-
## Implementing MVVM
263+
## Implementing MVVM - 25 min
254264

255-
In pairs complete [this activity](assignments/activity1.md)
265+
Individually complete [this activity](https://github.com/amelinagzz/mvvm-starter/tree/main)
256266

257267
<!-- > -->
258268

Slides/07-MVVM/README.html

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,62 +42,64 @@
4242
By the end of this lesson, you should be able to...
4343

4444
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
4747
- 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
5149
4. Implement basic examples of MVVM explored in this class
5250

5351
</script></section><section data-markdown><script type="text/template">
5452
### Model-View-ViewModel
5553

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.*__
5958
</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.
6160
</script></section><section data-markdown><script type="text/template">
62-
![mvvm](mvvm.png)
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.
6364
</script></section><section data-markdown><script type="text/template">
6465
### A Brief History
6566

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.
6768

6869
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.
6970
</script></section><section data-markdown><script type="text/template">
7071
### Components of MVVM
7172

7273
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.
7374

74-
MVVM takes the principle of **Separation of Concerns** one step further &mdash; 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 &mdash; 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+
![mvvm](mvvm.png)
7578
</script></section><section data-markdown><script type="text/template">
7679
Here is how the major components of MVVM relate to each other (and to MVC):
7780

7881
**View** &mdash; Responsible for displaying visual elements and controls on the screen. Typically, subclasses of `UIView`. </br>
7982
(Same as in MVC)
8083
</script></section><section data-markdown><script type="text/template">
81-
**Model** &mdash; Objects that hold app data; typically, `structs` or simple `classes`. (Same as in MVC)
84+
**Model** &mdash; Objects that hold app data; typically, structs or simple classes. (Same as in MVC)
8285
</script></section><section data-markdown><script type="text/template">
8386
**View Controller** &mdash; 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.
8487
</script></section><section data-markdown><script type="text/template">
85-
**View Model** &mdash; 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** &mdash; 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.
8689
</script></section><section data-markdown><script type="text/template">
8790
### Problems Addressed
8891

89-
Discuss with a partner:
92+
Share in the chat:
9093

9194
- Why do you think developers turn to this architecture?
9295

9396
- What are some limitations you've noticed while using MVC?
94-
9597
</script></section><section data-markdown><script type="text/template">
9698
MVVM can be used to solve two related development issues:
9799

98100
**"MVC: Massive View Controller"** &mdash; In MVC, where do you put functionality that does not belong neatly in either the View or the Model?
99101

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.
101103
</script></section><section data-markdown><script type="text/template">
102104
__*Example*__ </br>
103105
Data formatting is a common task. Imagine your app needs data &mdash; dates, currency, etc. &mdash; *formatted differently* for various user locales.
@@ -127,9 +129,17 @@
127129

128130
When the View Model knows nothing about View layout, testing each - separately or together - is much easier.
129131
</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.
131141

132-
Individually complete [this activity](assignments/activity1.md)
142+
1. **Identify the role** played by callbacks.
133143
</script></section><section data-markdown><script type="text/template">
134144
### Benefits
135145

@@ -209,9 +219,9 @@
209219

210220
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.
211221
</script></section><section data-markdown><script type="text/template">
212-
## Implementing MVVM
222+
## Implementing MVVM - 25 min
213223

214-
In pairs complete [this activity](assignments/activity1.md)
224+
Individually complete [this activity](https://github.com/amelinagzz/mvvm-starter/tree/main)
215225
</script></section><section data-markdown><script type="text/template">
216226
## After Class
217227

0 commit comments

Comments
 (0)