Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
MVVM
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Matt Diephouse
May 13, 2014
Programming
250
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
MVVM
Matt Diephouse
May 13, 2014
More Decks by Matt Diephouse
See All by Matt Diephouse
Noble Cocoa
mdiep
2
270
Static Libraries
mdiep
1
130
Other Decks in Programming
See All in Programming
仕様駆動開発の消費期限
watany
20
9.1k
Cloudflare is Agents
chimame
0
190
高専キャリア LT 発表内容
crysta1221
6
5k
AIと壁打ちしながら進めるコスト管理
fufuhu
2
1.8k
TSX の <Hoge<Fuga>> という構文に驚いた話 / tsx-type-argument-syntax
kanaru0928
0
240
いまどきの Codex で開発する visionOS アプリの開発スタイルについて
karad
0
170
仕様書を書く前にハーネスを作る - Agent Native開発は「探索を速く、判定を固く」
gotalab555
5
1.8k
typoなんかねぇよ
raspython3
0
620
AI駆動開発にグラフDBを重ねてみた
satoshi256kbyte
2
420
Flow は今どうなっているか
mizdra
PRO
0
770
AI Readyの正体はデータマネジメントだ メダリオン2.0の最前線
freee
PRO
0
450
AI Engineeringは、AIプロダクトだけのものか? 〜AIがソフトウェアを作る時代の新しい当たり前〜 / No AI in your product. AI Engineering in your development.
rkaga
5
550
Featured
See All Featured
エンジニアに許された特別な時間の終わり
watany
108
250k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
480
Art, The Web, and Tiny UX
lynnandtonic
304
22k
Discover your Explorer Soul
emna__ayadi
2
1.3k
Fashionably flexible responsive web design (full day workshop)
malarkey
408
67k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
230
The SEO Collaboration Effect
kristinabergwall1
1
540
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Facilitating Awesome Meetings
lara
57
7.1k
Transcript
MVVM Model – View – ViewModel
WHAT IS MVVM?
IT'S LIKE MVC, BUT DIFFERENT.
BUT IT'S NOT THAT DIFFERENT.
IT'S JUST DIFFERENT ENOUGH.
Example: IPHONE RECIPE APP A TABLE OF RECIPES GROUPED ALPHABETICALLY
MVC 1.0
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.fetchedResultsController.sections.count; } - (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section { id<NSFetchedResultsSectionInfo> info = self.fetchedResultsController.sections[section]; return info.numberOfObjects; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:...]; RCPRecipe *recipe = [self.fetchedResultsController objectAtIndexPath:indexPath]; cell.textLabel.text = [NSString stringWithFormat:@"%@ by %@", recipe.name, recipe.author.name]; return cell; }
MVC 1.0 ▸ Logic is tied to presentation ▸ It
can only be tested visually ▸ The code is verbose ▸ Tightly coupled to the database and model ▸ No separation of concerns
MVC 2.0
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.numberOfSections; } - (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section { return [self numberOfRecipesInSection:section]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:...]; RCPRecipe *recipe = [self recipeAtIndexPath:indexPath]; cell.textLabel.text = [NSString stringWithFormat:@"%@ by %@", recipe.name, recipe.author.name]; return cell; }
- (NSInteger)numberOfSections { return self.fetchedResultsController.sections.count; } - (NSInteger)numberOfRecipesInSection:(NSInteger)section { id<NSFetchedResultsSectionInfo>
info = self.fetchedResultsController.sections[section]; return info.numberOfObjects; } - (RCPRecipe *)recipeAtIndexPath:(NSIndexPath *)indexPath { return [self.fetchedResultsController objectAtIndexPath:indexPath]; }
MVC 2.0 ▸ Some logic could be tested in code
▸ Partial separation of concerns ▸ Reusable methods for getting model objects ▸ Slightly less verbose
MVC 2.0 ▸ Some logic is still tied to presentation
▸ Still requires visual testing ▸ Tightly coupled to the model
MVC 3.0
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.numberOfSections; } - (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section { return [self numberOfRecipesInSection:section]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:...]; RCPRecipe *recipe = [self recipeAtIndexPath:indexPath]; cell.textLabel.text = [self displayNameForRecipe:recipe]; return cell; }
MVC 3.0 ▸ Logic is testable in code ▸ Separation
of concerns
MVC 3.0 ▸ Tightly coupled to the model ▸ Testing
per-row logic requires an entire table view ▸ Logic could easily depend on view
MVVM
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.viewModel.numberOfSections; } - (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section { return [self.viewModel numberOfRecipesInSection:section]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:...]; RCPRecipeViewModel *viewModel = [self.viewModel viewModelAtIndexPath:indexPath]; cell.textLabel.text = viewModel.displayName; return cell; }
@interface RCPRecipeListViewModel : NSObject @property (assign, nonatomic) NSInteger numberOfSections; -
(NSInteger)numberOfRecipesInSection:(NSInteger)section; - (RCPRecipeViewModel *)viewModelAtIndexPath:(NSIndexPath *)indexPath; @end
@interface RCPRecipeViewModel : NSObject @property (copy, nonatomic) NSString *displayName; -
(instancetype)initWithRecipe:(RCPRecipe *)recipe; @end
MVVM ▸ Separation of concerns ▸ Testable in code ▸
Easy to test per-row logic ▸ Loosely coupled ▸ Independent of UIKit/AppKit ▸ Easily have different row types
Q & A