๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
IOS๐ŸŽ/์•„ํ‚คํ…์ณ

[์•„ํ‚คํ…์ณ] MVVM (clean-architecture) ์ด๋ž€

by Jouureee 2021. 12. 22.

์˜ค๋Š˜์€ ๊ฐ€์žฅ ๋งŽ์ด ์“ฐ์ด๋Š” MVVM ํŒจํ„ด์— ๋Œ€ํ•ด ๋‹ค์‹œ ์ •๋ฆฌ๋ฅผ ํ•ด๋ณด๋ ค๊ตฌ ํ•œ๋‹ค.

 

 

๊ธฐ์กด MVC ํŒจํ„ด์ด View์™€ Controller ๊ฐ„ ๋ถ„๋ฆฌ๊ฐ€ ๋˜์ง€ ์•Š์•„ ๋น„๋Œ€ํ•ด์ง„๋‹ค๋Š” ๋ฌธ์ œ์ ์ด ์žˆ์—ˆ๋‹ค.

MVVM ํŒจํ„ด์€ view, view model, model ์ด๋ ‡๊ฒŒ 3๊ฐ€์ง€ ๋ถ„๋ฆฌ๋ฅผ ํ†ตํ•ด ๊ทธ ๋ฌธ์ œ์ ์„ ํ•ด๊ฒฐํ•œ๋‹ค

์•ž์œผ๋กœ๋Š” model์˜ ๋ณ€ํ™”๋ฅผ view model์ด ์ฑ…์ž„์ง€๊ณ  view๋Š” model๊ฐ€ ์ง์ ‘์ ์ธ ์—ฐ๊ฒฐ ์—†์ด view model์„ ํ†ตํ•ด view ์ž์‹ ์„ ์—…๋ฐ์ดํŠธ ํ•œ๋‹ค. 

 

์—ฌ๊ธฐ๊นŒ์ง€๋Š” ๋‚ด๊ฐ€ ์•Œ๊ณ  ์žˆ๋˜ MVVM ํŒจํ„ด์ด์—ˆ๊ตฌ MVVM ํŒจํ„ด์— Repository ?? ๋ฅผ ์“ฐ๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ฐœ๊ฒฌํ•ด์„œ ๊ถ๊ธˆํ•ด์„œ ์ •๋ฆฌ ํ•ด๋ณด๋ ค๊ณ  ๋งˆ์Œ ๋จน๊ฒŒ ๋˜์—ˆ๋‹ค. 

์•„๋ž˜ ๊นƒํ—™์„ ์ฐธ๊ณ ํ•˜์—ฌ ์ •๋ฆฌํ•ด๋ณด๋ ค๊ตฌ ํ•œ๋‹ค.

https://github.com/kudoleh/iOS-Clean-Architecture-MVVM

 

GitHub - kudoleh/iOS-Clean-Architecture-MVVM: Template iOS app using Clean Architecture and MVVM. Includes DIContainer, FlowCoor

Template iOS app using Clean Architecture and MVVM. Includes DIContainer, FlowCoordinator, DTO, Response Caching and one of the views in SwiftUI - GitHub - kudoleh/iOS-Clean-Architecture-MVVM: Tem...

github.com

 

์šฐ์„  ํด๋ฆฐ ์•„ํ‚คํ…์ณ๋ž€ ? (ํด๋ฆฐ ์ฝ”๋“œ .. ๐Ÿ‘)

Robert C Martin(Uncle Bob)๋‹˜์ด ์ œ์•ˆํ•˜์…จ๋Š”๋ฐ, ์•„๋ž˜์‚ฌ์ง„์ฒ˜๋Ÿผ ์•ฑ ์•„ํ‚คํ…์ณ๋ฅผ ๊ตฌ์„ฑํ•œ ๊ฒƒ์„ Clean Architecture๋ผ๊ณ  ํ•œ๋‹ค.

https://blog.coderifleman.com/2017/12/18/the-clean-architecture/

 

The Clean Architecture

์ด ๋ฌธ์„œ๋Š” ๋กœ๋ฒ„ํŠธ C. ๋งˆํ‹ด์˜ The Clean Architecture๋ฅผ ํ•œ๊ตญ์–ด๋กœ ๋ฒˆ์—ญํ•œ ๋ฌธ์„œ์ž…๋‹ˆ๋‹ค. ๊ทธ์˜ ์ข‹์€ ์˜๋„์™€ ์ƒ๊ฐ์ด ๋„๋ฆฌ ์ „ํŒŒ๋˜๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค

blog.coderifleman.com

์ด ํ•œ๊ธ€  ๋ฒˆ์—ญ๋ณธ์„ ์ฐธ๊ณ ํ•˜์˜€๋‹ค !

 

์šฐ์„  ์›๋“ค์€ ๊ณ„์ธต์„ ๋„๊ณ  ์žˆ๋‹ค.

์•ˆ์ชฝ์˜ ์›๋“ค์€ ์ •์ฑ…(Policy)์ด๊ณ  ๋ฐ”๊นฅ ์›๋“ค์€ ๋ฉ”์ปค๋‹ˆ์ฆ˜(Mechanism)์ด๋‹ค.

์ด ์›๋“ค ์‚ฌ์ด์—๋Š” ๊ทœ์น™์ด ์กด์žฌํ•˜๋Š”๋ฐ ๋ฐ”๊นฅ์ชฝ ์›์—์„œ ์„ ์–ธ๋œ ๊ทœ์น™์€ ์•ˆ์ชฝ ์›์œผ๋กœ ๋“ค์–ด์˜ฌ ์ˆ˜ ์—†๋‹ค. ์ฆ‰ ์ฐธ์กฐ, ์‚ฌ์šฉ ๋ชจ๋‘ ์•ˆ๋œ๋‹ค.

์ด ๊ทœ์น™์„ ํ†ตํ•ด์„œ ์˜์กด ๊ด€๊ณ„๋ฅผ ์œ ์ง€ํ• ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค.

 

1.์—”ํ‹ฐํ‹ฐ

์—”ํ‹ฐํ‹ฐ๋Š” ๋Œ€๊ทœ๋ชจ ํ”„๋กœ์ ํŠธ ๋ ˆ๋ฒจ์˜ ๋น„์ง€๋‹ˆ์Šค ๊ทœ์น™์„ ์บก์Šํ™” ํ•œ๋‹ค. ๋Œ€๊ทœ๋ชจ ํ”„๋กœ์ ํŠธ๊ฐ€ ์•„๋‹Œ ํ•˜๋‚˜์˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ž‘์„ฑํ•  ๋ฟ์ด๋ผ๋ฉด ์—”ํ‹ฐํ‹ฐ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋น„์ง€๋‹ˆ์Šค ๊ฐ์ฒด๋กœ์„œ ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ด๊ณ  ๊ณ ์ˆ˜์ค€์˜ ๊ทœ์น™์„ ์ง€๋‹Œ๋‹ค. ๋ณ€๋™ ๊ฐ€๋Šฅ์„ฑ์ด ์ ๋‹ค. 

 

2. ์œ ์ฆˆ์ผ€์ด์Šค

์ด ๊ณ„์ธต์˜ ์†Œํ”„ํŠธ์›จ์–ด๋Š” ์—”ํ‹ฐํ‹ฐ๋กœ๋ถ€ํ„ฐ์˜ ๋ฐ์ดํ„ฐ ํ๋ฆ„์„ ์กฐํ•ฉํ•œ๋‹ค. ์ด ๊ณ„์ธต์œผ๋กœ๋ถ€ํ„ฐ์˜ ์ˆ˜์ • ์‚ฌํ•ญ์ด ์—”ํ‹ฐํ‹ฐ์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์„ ๊ฒƒ์œผ๋กœ ๊ธฐ๋Œ€ํ•˜๋ฉฐ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, UI ๋˜๋Š” ๊ณตํ†ต์˜ ํ”„๋ ˆ์ž„์›Œํฌ์˜ ๋ณ€๊ฒฝ์œผ๋กœ๋ถ€ํ„ฐ ์˜ํ–ฅ ๋ฐ›์ง€ ์•Š์„ ๊ฒƒ๋„ ๊ธฐ๋Œ€ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์กฐ์ž‘์— ๋Œ€ํ•œ ๋ณ€๊ฒฝ์€ ์œ ์ฆˆ์ผ€์ด์Šค์— ์˜ํ–ฅ์ด ์žˆ๊ณ  ์ด ๊ณ„์ธต์˜ ์†Œํ”„ํŠธ์›จ์–ด์— ์˜ํ–ฅ์ด ์žˆ์„ ๊ฒƒ์„ ๊ธฐ๋Œ€ํ•œ๋‹ค. 

 

3. ์ธํ„ฐํŽ˜์ด์Šค ์–ด๋Œ‘ํ„ฐ

์ด ๊ณ„์ธต์˜ ์†Œํ”„ํŠธ์›จ์–ด๋Š” ์–ด๋Œ‘ํ„ฐ์˜ ์ง‘ํ•ฉ์ด๋‹ค. ์œ ์ฆˆ์ผ€์ด์Šค์™€ ์—”ํ‹ฐํ‹ฐ์— ์žˆ์–ด ์šฉ์ดํ•œ ํ˜•์‹์œผ๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค๋‚˜ ์›น ๋“ฑ ์™ธ๋ถ€ ๊ธฐ๋Šฅ์— ์šฉ์ดํ•œ ํ˜•์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€ํ™˜ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด GUI์˜ MVC ์•„์ผ€ํ…์ณ๋ฅผ ์™„์ „ํžˆ ๋‚ดํฌํ•œ๋‹ค. ํ”„๋ ˆ์  ํ„ฐ, ๋ทฐ, ์ปจํŠธ๋กค๋Ÿฌ ๋ชจ๋‘ ์—ฌ๊ธฐ์— ์†ํ•œ๋‹ค. 

 

4. ํ”„๋ ˆ์ž„์›Œํฌ์™€ ๋“œ๋ผ์ด๋ฒ„

๊ฐ€์žฅ ๋ฐ”๊นฅ์ชฝ์˜ ๊ณ„์ธต์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋‚˜ ์›น ํ”„๋ ˆ์ž„ ์›Œํฌ ๋“ฑ ์ผ๋ฐ˜์ ์œผ๋กœ ํ”„๋ ˆ์ž„์›Œํฌ๋‚˜ ๋„๊ตฌ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค. ex) Network, CoreData

 

์‚ฌ์‹ค ์ž˜ ์ดํ•ด๊ฐ€ ๊ฐ€์ง„ ์•Š์ง€๋งŒ ์•„๋Š” ๋‹จ์–ด๊ฐ€ ๋‚˜์˜ค๋Š” ๋ถ€๋ถ„์€ ๋“œ๋ฌธ ๋“œ๋ฌธ ์—ฐ๊ฒฐํ•˜๋‹ˆ ์•Œ๊ฒƒ ๊ฐ™๊ธฐ๋„ ํ•˜๊ณ  ๊ทธ๋ ‡๋‹ค.

https://amara.org/ko/videos/0AtjY87egE3m/url/1216370/

 

Video Subtitling, Captioning and Subtitle Translation - Amara Subtitling

The FCCChairman's AAA The Chairman's Awards for Advancement in Accessibility is an FCC program recognizing products, services, standards and other innovative developments that improve the experience of people with disabilities.

amara.org

์ž๋ง‰ ๋™์˜์ƒ๋„ ์žˆ๋‹ค ํ•˜๋‹ˆ ๋‚˜์ค‘์—  ์ฐธ๊ณ ์šฉ์œผ๋กœ ๋ณด์•„์•ผ๊ฒ ๋‹ค.

 

 

์˜ˆ์ œ ์ฝ”๋“œ ๋ถ„์„ํ•ด๋ณด๊ธฐ

์˜ˆ์ œ ์ฝ”๋“œ์˜ ํด๋” ๊ณ„์ธต ๊ตฌ์กฐ์ด๋‹ค. 

ํฌ๊ฒŒ ์˜์—ญ์€ Domain, Presentation, Data ์ด๋ ‡๊ฒŒ ์„ธ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋‰œ๋‹ค.

 

Domain ๋ถ€๋ถ„์—๋Š” Entities, UseCase, interface๊ฐ€ ์žˆ๋Š”๋ฐ interface๋Š” ์ž๋ฐ” ์ƒ์†๊ด€๊ณ„์—์„œ ๋ณด๋˜ ๊ทธ interface์ธ๊ฑฐ ๊ฐ™๊ณ  repositories ํด๋”๋ฅผ ๋ณด๋ฉด Data ์•„๋ž˜ repositories์—์„œ ์‚ฌ์šฉํ•  ํด๋ž˜์Šค๋“ค์— ๋Œ€ํ•œ ํ”„๋กœํ† ์ฝœ์ด ์ •์˜๋˜์–ด ์žˆ๋‹ค.

 

๊ณ„์ธต ๊ตฌ์กฐ๋ฅผ ๋‹ค๋ฅด๊ฒŒ ๊ตฌ์กฐํ™” ํ•˜๋ฉด ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™๋‹ค.

user๋Š” Presentation ์˜ View์™€ interaction ํ•˜๊ณ  ๊ทธ ์‚ฌ์ด๋ฅผ  Presenter๊ฐ€ ๋งค๊ฐœํ•œ๋‹ค. ์ด ์—ญํ• ์„ MVVM ํŒจํ„ด์—์„   ViewModel์ด ์ฃผ๋„ํ•œ๋‹ค. viewModel์—์„  ํ•˜๋‚˜ ์ด์ƒ์˜ usecase๋ฅผ excute ํ•˜๊ธฐ ๋•Œ๋ฌธ์— Domain Layer์™€ interactํ•  ์ˆ˜  ์žˆ๋‹ค. 

 

Data layer์—์„  API, DB๋ฅผ ๋‹ด๋‹นํ•œ๋‹ค.

 

Domain layer์— ์žˆ๋Š” repositories protocol์„ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค๋“ค์ด ์žˆ๊ณ  DB๋ฅผ ๊ตฌํ˜„ํ•œ Core Data๋“ค์ด ์กด์žฌํ•œ๋‹ค.

 

์ด ์ •๋ฆฌํ•˜๋ฉด ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™๋‹ค.

Domain Use Case์™€ Data Repository ์‚ฌ์ด์— Repository Interface Dependency Inversion์ด๋ผ๋Š”๊ฒŒ ์žˆ๋‹ค.

 

ํ•œ๊ตญ๋ง๋กœ ํ•˜๋ฉด

์˜์กด์„ฑ ์—ญ์ „ ์›์น™ 

์ด๋‹ค.

 

1. ๊ณ ์ˆ˜์ค€ ๋ชจ๋“ˆ์€ ์ €์ˆ˜์ค€ ๋ชจ๋“ˆ์— ์˜์กดํ•ด์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค.

2. ์ถ”์ƒํ™”๋Š” ์„ธ๋ถ€ ์‚ฌํ•ญ์— ์˜์กดํ•ด์„œ๋Š” ์•ˆ๋ฉ๋‹ˆ๋‹ค.

 

์ฆ‰ ์šฐ๋ฆฌ๊ฐ€ Repository๋ฅผ ๊ตฌํ˜„ํ• ์‹œ Domain -> Data์˜  ์˜์กด์„ฑ์„ ๋Š์Šจํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด protocol ํ˜•์‹์œผ๋กœ ๋ณ€๊ฒฝ์— ํ™•์žฅ์„ฑ์„ ์šฉ์ดํ•˜๊ฒŒ ํ•œ ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.

 

 

๋Œ“๊ธ€