Published on

๐ŸŽ Swift - MVVM, Clean Architecture.02

Authors
  • avatar
    Name
    ์ด์ฐฝ์ค€
    Twitter

์ด๋ฒˆ ํฌ์ŠคํŠธ์—์„œ๋Š” ์ œ ํ”„๋กœ์ ํŠธ๋ฅผ ์˜ˆ์‹œ๋กœ iOS ํ”„๋กœ์ ํŠธ์— MVVM+Clean Architecture ๊ตฌ์กฐ๋ฅผ ์ ์šฉํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!

์ €๋ฒˆ ํฌ์ŠคํŒ…์„ ์ž‘์„ฑํ•˜๊ณ ๋„ ํ—ท๊ฐˆ๋ฆฌ๋Š” ๋ถ€๋ถ„๋“ค์ด ๋งŽ์•„์„œ ๊ณ„์†ํ•ด์„œ ์ •๋ณด๋“ค์„ ์ฐพ์•„๋ดค์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ค๊ฐ€ ์•„์ฃผ ์ข‹์€ ํฌ์ŠคํŠธ๋ฅผ ๋ฐœ๊ฒฌํ–ˆ๋Š”๋ฐ, ์˜ค๋Š˜์€ ํ•ด๋‹น ํฌ์ŠคํŠธ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ œ ํฌ์ŠคํŠธ๋ฅผ ๋ณด์‹œ๋Š” ๋ถ„๋“ค๋„ ์•„๋ž˜ ํฌ์ŠคํŠธ๋ฅผ ์ž์„ธํžˆ ์ฝ์–ด๋ณด๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค ๐Ÿ‘

Clean Architecture and MVVM on iOS

MVVM + Clean Architecture

MVVM + Clean Layers

ํ•ด๋‹น ๊ฒŒ์‹œ๋ฌผ์„ ๋ณด๋ฉด ์œ„์™€ ๊ฐ™์ด ํฌ๊ฒŒ ์„ธ ๊ฐ€์ง€ ๋ ˆ์ด์–ด๋กœ ๊ตฌ์„ฑ์„ ๋‚˜๋ˆ„์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์˜๋ฌธ์ ์ด ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ €๋ฒˆ ํฌ์ŠคํŠธ์—์„œ๋Š” 4๊ฐ€์ง€๋กœ ๋‚˜๋‰˜์—ˆ๋Š”๋ฐ Clean Architecture ๊ตฌ์กฐ๊ฐ€ ์•„๋‹Œ๊ฑฐ ์•„๋‹Œ๊ฐ€์š”?

Clean Architecture์˜ ํ•ต์‹ฌ์€ ์˜์กด์„ฑ์— ์žˆ์Šต๋‹ˆ๋‹ค. ์•ˆ์ชฝ ๋ ˆ์ด์–ด๋ถ€ํ„ฐ ๋ฐ”๊นฅ ๋ ˆ์ด์–ด๋กœ์˜ ์˜์กด์„ฑ ๋ฐฉํ–ฅ์ด ์žˆ์ง€๋งŒ ์•Š์œผ๋ฉด ๋ ˆ์ด์–ด์˜ ๊ฐœ์ˆ˜๋‚˜ ๊ตฌ์กฐ๋Š” ์•„๋ฌด ์ƒ๊ด€์ด ์—†์ฃ . ๋”ฐ๋ผ์„œ ์œ„์™€ ๊ฐ™์€ ๊ตฌ์กฐ๋„ ์•„๋ฌด ๋ฌธ์ œ๊ฐ€ ์—†๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค!

์œ„ ๊ฒŒ์‹œ๋ฌผ์— ๋”ฐ๋ฅธ ๋ ˆ์ด์–ด์˜ ์ข…๋ฅ˜๋Š” ์•„๋ž˜๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • Presentation Layer
  • Domain Layer
  • Data Layer

Domain Layer

Domain Layer๋Š” ๊ฐ€์žฅ ์•ˆ์ชฝ์˜ ๋ ˆ์ด์–ด์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์•„๋ฌด๋Ÿฐ ์˜์กด์„ฑ์„ ๊ฐ€์ง€์ง€ ๋ง์•„์•ผํ•˜๊ณ , ๋…๋ฆฝ๋˜์–ด ์žˆ์–ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์ด๋กœ์„œ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ด์ ์œผ๋กœ๋Š” ์•„๋ฌด๋ก  ์ข…์†์„ฑ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ํ…Œ์ŠคํŠธํ•˜๊ธฐ ๋งค์šฐ ๊ฐ„ํŽธํ•˜๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ํŒŒ์ผ์˜ ์ˆ˜์ •์ด๋‚˜ ์ถ”๊ฐ€ ์—†์ด ์˜ค์ง Domain Layer์—๋งŒ ์ง‘์ค‘ํ•ด๋„ ๊ฒฐ๊ณผ๊ฐ€ ๋„์ถœ๋˜์ฃ .

Entities, Use Cases, Repository Interfaces๋“ค์ด Domain Layer์— ์†ํ•ฉ๋‹ˆ๋‹ค.

Presentation Layer

Presentation Layer์—๋Š” ๋ง ๊ทธ๋Œ€๋กœ UI ์š”์†Œ๋“ค์ด ์†ํ•ฉ๋‹ˆ๋‹ค. ๋Œ€ํ‘œ์ ์œผ๋กœ View Model์ด ์—ฌ๊ธฐ์— ์†ํ•˜์—ฌ Use Cases์˜ ํ•จ์ˆ˜๋“ค์„ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

Presentation Layer๋Š” ์˜ค์ง Domain Layer์—๋งŒ ์ข…์†๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

Data Layer

Data Layer์—๋Š” Repository์™€ Data Sources๋“ค์ด ์†ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ Repository๋Š” ์™ธ๋ถ€์˜ Data Sources๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ฌ ๋•Œ ์‚ฌ์šฉ๋˜๊ณ , Data Sources๋“ค์€ ๋กœ์ปฌ ๋ฐ์ดํ„ฐ๋“ค์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

Data Layer ๋˜ํ•œ ์˜ค์ง Domain Layer์—๋งŒ ์ข…์†๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

โœ”๏ธ ์ ์šฉํ•˜๊ธฐ

์œ„ ํฌ์ŠคํŠธ์™€ ์ œ๊ฐ€ ๋ฐฐ์›Œ์˜จ๋Œ€๋กœ ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚˜์™€์•ผํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ๊ตฌ์กฐ๊ฐ€ ์ด๋ ‡๊ฒŒ ๋˜๋ฉด Use Case๊ฐ€ Repository๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๋ฐ, ์ฐธ์กฐ ๊ด€๊ณ„๋Š” ๋ฐ˜๋Œ€๋กœ ๋˜์–ด ์žˆ๋Š” ์•„์ฃผ ์ž˜๋ชป๋œ ํ˜„์ƒ์ด ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์•ˆ์ชฝ ๋ ˆ์ด์–ด๊ฐ€ ๋ฐ”๊นฅ ์ชฝ ๋ ˆ์ด์–ด๋ฅผ ์ฐธ์กฐํ•˜๋‹ค๋‹ˆ, Clean Architecture์˜ ๊ทœ์น™์„ ๊นจ๋ถ€์ˆ˜๊ณ  ์žˆ๋„ค์š” ๐Ÿ˜ซ

ํ•˜์ง€๋งŒ Use Case๋Š” ๊ผญ Repository์— Request๋ฅผ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿด ๋• ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ์š”?

๋ฐ”๋กœ ์œ„์™€ ๊ฐ™์ด ์ค‘๊ฐ„์— ํ•œ ๋‹จ๊ณ„๋ฅผ ๋” ๊ฑฐ์น˜๊ฒŒ ๋งŒ๋“ค๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์œ„ ๋ ˆ์ด์–ด ์„ค๋ช…์—์„œ Domain Layer์— Repository Interface๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์—ˆ์ฃ ? ์ด ์นœ๊ตฌ๊ฐ€ ๋ฐ”๋กœ ์—ฌ๊ธฐ์— ์“ฐ์ž…๋‹ˆ๋‹ค.

Repository Interface๋Š” ํ”„๋กœํ† ์ฝœ์˜ ํ˜•ํƒœ๋กœ์„œ Use Case์™€ ๊ฐ™์€ ๋ ˆ์ด์–ด์— ์†ํ•ด์žˆ์ง€๋งŒ, ์‹ค์ œ ๊ตฌํ˜„์€ Data ๋ ˆ์ด์–ด์— ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๋ฐ์ดํ„ฐ์˜ ํ๋ฆ„์„ ์‚ดํŽด๋ณด๊ณ  ์‹ค์ œ ๊ตฌํ˜„์— ๋“ค์–ด๊ฐ€๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

  1. View๊ฐ€ View Model์˜ ํ•จ์ˆ˜๋ฅผ ์š”์ฒญํ•œ๋‹ค.
  2. View Model์ด Use Case๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
  3. Use Case๊ฐ€ User์™€ Repository๋กœ๋ถ€ํ„ฐ ์–ป์€ ๋ฐ์ดํ„ฐ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ๋„์ถœํ•ฉ๋‹ˆ๋‹ค.
  4. ์ด ๋•Œ, Repository๋Š” ๋„คํŠธ์›Œํฌ(ex: API)์™€ Local DB(ex: Realm)๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์™€ Use Case์— ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
  5. Use Case์—์„œ ๋„์ถœ๋œ ๊ฒฐ๊ณผ๋ฅผ ๊ฑฐ๊พธ๋กœ View์— ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.