๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ–ฅ CS/OOP

[Swift] DI(์˜์กด์„ฑ ์ฃผ์ž…) in Swift

์˜์กด์„ฑ(Dependency)

๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ์˜์กด์„ฑ(Dependency)์€ ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ์ฒด ์‚ฌ์ด์— ์˜์กด ๊ด€๊ณ„๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค.

์ฆ‰, ์˜์กดํ•˜๋Š” ๊ฐ์ฒด๊ฐ€ ์ˆ˜์ •๋˜๋ฉด, ๋‹ค๋ฅธ ๊ฐ์ฒด๋„ ์˜ํ–ฅ์„ ๋ฐ›๋Š”๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

์˜ˆ์‹œ๋กœ ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ๋ด…์‹œ๋‹ค.

 

struct Coffee {
    func drink() {
        print("์ปคํ”ผ๋ฅผ ๋งˆ์‹ ๋‹ค")
    }
}

struct Programmer {
    private let coffee = Coffee()

    func startProgramming() {
        self.coffee.drink()
    }
}

 

Programmer๊ฐ์ฒด๋Š” Coffee๊ฐ์ฒด๋ฅผ ์ธ์Šคํ„ด์Šค๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ,
Programmer๊ฐ์ฒด์˜ startProgramming()์ด ํ˜ธ์ถœ๋˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Coffee ๊ตฌ์กฐ์ฒด๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.
์ด ๋•Œ Programmer๊ฐ์ฒด๋Š” Coffee๊ฐ์ฒด์— ์˜์กด์„ฑ์„ ๊ฐ€์ง„๋‹ค ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
๋งŒ์•ฝ Coffee ๊ฐ์ฒด์— ์ค‘์š”ํ•œ ์ˆ˜์ •์ด๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๋ฉด? Programmer๊ฐ์ฒด๋„ ์˜ํ–ฅ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด Coffee์˜ drink()๋ผ๋Š” ๋ฉ”์†Œ๋“œ์˜ ๊ฐ’์ด ๋ฐ”๋€Œ๊ฑฐ๋‚˜ ๋ฉ”์†Œ๋“œ ์ž์ฒด๊ฐ€ ์—†์–ด์ง„๋‹ค๋ฉด
Programmer์˜ startProgramming() ๋ฉ”์†Œ๋“œ์˜ ์ˆ˜์ •๋„ ํ•„์š”ํ•˜๊ฒ ์ฃ ..
์ด๋ ‡๊ฒŒ ์˜์กด์„ฑ์„ ๊ฐ€์ง€๋Š” ์ฝ”๋“œ๊ฐ€ ๋งŽ์•„์ง„๋‹ค๋ฉด, ์žฌํ™œ์šฉ์„ฑ์ด ๋–จ์–ด์ง€๊ณ  ๋งค๋ฒˆ ์˜์กด์„ฑ์„ ๊ฐ€์ง€๋Š” ๊ฐ์ฒด๋“ค์„ ํ•จ๊ป˜ ์ˆ˜์ •ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋‚˜์˜จ ๊ฐœ๋…์ด ๋ฐ”๋กœ Dependency Injection, ์˜์กด์„ฑ ์ฃผ์ž…์ž…๋‹ˆ๋‹ค.

DI(์˜์กด์„ฑ ์ฃผ์ž…)๋ฅผ ํ•ด์•ผ ํ•˜๋Š” ์ด์œ 

DI๋กœ ํ”„๋กœ๊ทธ๋žจ์„ ์„ค๊ณ„ ํ–ˆ์„ ๋•Œ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ด์ ์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • Unit Test๊ฐ€ ์šฉ์ดํ•ด์ง„๋‹ค.
  • ์ฝ”๋“œ์˜ ์žฌํ™œ์šฉ์„ฑ์„ ๋†’์—ฌ์ค€๋‹ค.
  • ๊ฐ์ฒด ๊ฐ„์˜ ์˜์กด์„ฑ(์ข…์†์„ฑ)์„ ์ค„์ด๊ฑฐ๋‚˜ ์—†์•จ ์ˆ˜ ์žˆ๋‹ค.
  • ๊ฐ์ฒด ๊ฐ„์˜ ๊ฒฐํ•ฉ๋„์ด ๋‚ฎ์ถ”๋ฉด์„œ ์œ ์—ฐํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

DI(Dependency Injection) - ์˜์กด์„ฑ ์ฃผ์ž…

์ฃผ์ž…(Injection) ์€ ์™ธ๋ถ€์—์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์„œ ๋„ฃ๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

 

struct Coffee {
    var name: String
    
    func drink() {
        print("\(name)๋ฅผ ๋งˆ์‹ ๋‹ค")
    }
}

struct Programmer {
    private let coffee: Coffee
    
    init(coffee: Coffee) {
        self.coffee = coffee
    }
    
    func startProgramming() {
        self.coffee.drink()
    }
}

let americano = Coffee(name: "์•„๋ฉ”๋ฆฌ์นด๋…ธ")
let programmer = Programmer(coffee: americano)
programmer.startProgramming()

 

์œ„์˜ ์ฝ”๋“œ๋Š” Coffee๊ฐ์ฒด๋ฅผ ์™ธ๋ถ€์—์„œ ์ƒ์„ฑํ•˜๊ณ  Programmer ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ๋„ฃ์–ด์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
์ด๋ ‡๊ฒŒ ์™ธ๋ถ€์—์„œ ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ์ž ๋“ฑ์„ ํ™œ์šฉํ•ด์„œ ๋„ฃ์–ด์ฃผ๋Š” ๊ฒƒ์„ ์ฃผ์ž…(injection)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ, ์ฃผ์ž…๋งŒ์œผ๋กœ ์˜์กด์„ฑ์ด ์ค„์–ด๋“ค์—ˆ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”?
๋งŒ์•ฝ Coffee ๊ฐ์ฒด์˜ drink() ๋ฉ”์„œ๋“œ๊ฐ€ ๋ฐ”๋€๋‹ค๋ฉด? Programmer์˜ startProgramming() ๋ฉ”์„œ๋“œ ๋˜ํ•œ ์˜ํ–ฅ์„ ๋ฐ›์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์™ธ๋ถ€์—์„œ ์˜์กด์„ฑ์„ ์ฃผ์ž…ํ•˜๋Š” ๊ฒƒ ๋งŒ์œผ๋กœ๋Š” ์•„์ง Programmer๊ฐ์ฒด์˜ Coffee ๊ฐ์ฒด์— ๋Œ€ํ•œ ์˜์กด์„ฑ์„ ์ค„์˜€๋‹ค๊ณ  ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

Swift์—์„œ๋Š” ์˜์กด ๊ด€๊ณ„ ์—ญ์ „ ๋ฒ•์น™(DIP: Dependency Injection Principal) ์ด ์ค‘์š”ํ•œ ๊ฐœ๋…์ž…๋‹ˆ๋‹ค.
์˜์กด ๊ด€๊ณ„ ์—ญ์ „ ๋ฒ•์น™์œผ๋กœ ์˜์กด ๊ด€๊ณ„๋ฅผ ๋ถ„๋ฆฌ์‹œ์ผœ ๊ฐ์ฒด ๊ฐ„์˜ ๊ฒฐํ•ฉ๋„๋ฅผ ๋‚ฎ์ถœ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

DIP(Dependency Inversion Principle) - ์˜์กด ๊ด€๊ณ„ ์—ญ์ „ ๋ฒ•์น™

DIP, ์˜์กด ๊ด€๊ณ„ ์—ญ์ „ ๋ฒ•์น™์€ ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์„ค๊ณ„์˜ ๋‹ค์„ฏ๊ฐ€์ง€ ๊ธฐ๋ณธ ์›์น™(SOLID)์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. (SOLID์˜ D)
์˜์กด ๊ด€๊ณ„ ์—ญ์ „ ๋ฒ•์น™์€ ์ƒ์œ„ ๊ณ„์ธต(์ •์ฑ… ๊ฒฐ์ •)์ด ํ•˜์œ„ ๊ณ„์ธต(์„ธ๋ถ€ ์‚ฌํ•ญ)์— ์˜์กดํ•˜๋Š” ์ „ํ†ต์ ์ธ ์˜์กด๊ด€๊ณ„๋ฅผ ๋ฐ˜์ „์‹œํ‚ด์œผ๋กœ์จ

์ƒ์œ„ ๊ณ„์ธต์ด ํ•˜์œ„ ๊ณ„์ธต์˜ ๊ตฌํ˜„์œผ๋กœ๋ถ€ํ„ฐ ๋…๋ฆฝ๋˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค.

 

์ด ๋ฒ•์น™์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค.
์ถ”์ƒํ™” ๋œ ๊ฒƒ(์ƒ์œ„ ๋ชจ๋“ˆ)์€ ๊ตฌ์ฒด์ ์ธ ๊ฒƒ(ํ•˜์œ„ ๋ชจ๋“ˆ)์— ์˜์กดํ•˜๋ฉด ์•ˆ๋˜๊ณ  ๊ตฌ์ฒด์ ์ธ ๊ฒƒ(ํ•˜์œ„ ๋ชจ๋“ˆ)์ด ์ถ”์ƒํ™”๋œ ๊ฒƒ(์ƒ์œ„ ๋ชจ๋“ˆ)์— ์˜์กด ํ•ด์•ผํ•œ๋‹ค.
์ฆ‰, ๊ตฌ์ฒด์ ์ธ ๊ฐ์ฒด๋Š” ์ถ”์ƒํ™”๋œ ๊ฐ์ฒด์— ์˜์กด ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด DIP์˜ ํ•ต์‹ฌ์ž…๋‹ˆ๋‹ค.

 

Swift์—์„œ๋Š” Protocol์„ ์ด์šฉํ•ด ์ถ”์ƒ์ ์ธ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
Protocol์„ ํ™œ์šฉํ•ด ์˜์กด์„ฑ ์ฃผ์ž…์„ ๊ตฌํ˜„ํ•ด๋ด…์‹œ๋‹ค.

 

์•ž์˜ ์˜ˆ์‹œ์ฝ”๋“œ๋“ค์€ Programmer ๊ฐ์ฒด๊ฐ€ Coffee ๊ฐ์ฒด(Programmer → Coffee)์— ์˜์กดํ•˜๋Š” ๊ตฌ์กฐ์˜€๋‹ค๋ฉด,

์˜์กด ๊ด€๊ณ„ ์—ญ์ „ ๋ฒ•์น™์—์„œ๋Š” ์–ด๋–ค ์ถ”์ƒํ™”๋œ ์ธํ„ฐํŽ˜์ด์Šค(Swift์—์„œ๋Š” ํ”„๋กœํ† ์ฝœ)์— ๋‘ ๊ฐ์ฒด๊ฐ€ ๋ชจ๋‘ ์˜์กดํ•˜๊ณ  ์žˆ๋Š” ๊ตฌ์กฐ(Coffee → ํ”„๋กœํ† ์ฝœ ← Programmer)๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

protocol Drink {
  func drink()
}
struct Coffee: Drink {
  func drink() {
    print("์•„๋ฉ”๋ฆฌ์นด๋…ธ๋ฅผ ๋งˆ์‹ญ๋‹ˆ๋‹ค.")
  }
}

struct Programmer {
  private let coffee: Coffee
	
  init(coffee: Coffee) {
    self.coffee = coffee
  }
	
  func startProgramming() {
    self.coffee.drink()
  }
}

let americano = Coffee()
let programmer = Programmer(coffee: americano)
programmer.startProgramming()

 

Drinkํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•œ Coffee๊ฐ์ฒด์— drink()๊ฐ€ ์—†๋‹ค๋ฉด Coffee๋ฅผ ์„ ์–ธํ•  ๋•Œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
Programmer์— ๋Œ€ํ•œ ์ œ์–ด์˜ ์ฃผ์ฒด๊ฐ€ Coffee์— ์žˆ๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ํ”„๋กœํ† ์ฝœ์— ์žˆ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
์ฆ‰, ์ƒ์œ„ ๊ฐ์ฒด๊ณผ ํ•˜์œ„ ๊ฐ์ฒด ๋ชจ๋‘ ํ”„๋กœํ† ์ฝœ์— ์˜์กดํ•ด์žˆ์–ด ๋‘ ๊ฐ์ฒด ์„œ๋กœ์— ๋Œ€ํ•œ ์˜์กด ๊ด€๊ณ„๋ฅผ ๋…๋ฆฝ์‹œํ‚จ ์ƒํƒœ์ž…๋‹ˆ๋‹ค.
์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•œ๋‹ค๋ฉด ๋‘ ๊ฐ์ฒด๋Š” ๊ฑฐ์˜ ๋…๋ฆฝ์ ์ธ ๊ฐ์ฒด๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์—,
ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค๊ณ  ํ•ด์„œ ์ƒ๋Œ€ ๊ฐ์ฒด๋ฅผ ํ•จ๊ป˜ ์ˆ˜์ •ํ•ด์•ผํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


[์ฐธ๊ณ  ์ž๋ฃŒ]

 

[Swift] ์˜์กด์„ฑ ์ฃผ์ž… (DI) ์ด๋ž€?

ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ธฐ์กด ์ฝ”๋“œ์— ์˜์กด์„ฑ์„ ๊นจ๋Š” ๊ฒƒ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ข…์†์„ฑ์ด ๊ฐ์†Œํ•˜๋ฉด ์ˆ˜์ •์— ๋ฏผ๊ฐํ•˜์ง€ ์•Š๊ณ , ์œ ์—ฐ์„ฑ๊ณผ ํ™•์žฅ์„ฑ์ด ๋†’์•„์ ธ ํ…Œ์ŠคํŠธ์— ์šฉ์ดํ•ด์ง€๋Š” ์žฅ์ ์ด ์žˆ์Šต

silver-g-0114.tistory.com

 

(Swift) Dependency Injection, ์˜์กด์„ฑ ์ฃผ์ž…์ด๋ž€? (feat. DIP)

Dependency Injection, ์˜์กด์„ฑ ์ฃผ์ž…..

80000coding.oopy.io

 

Swift ์—์„œ ์˜์กด์„ฑ ์ฃผ์ž…

์•ˆ๋…•ํ•˜์„ธ์š”. ๋„๋ฏธ๋‹‰์ž…๋‹ˆ๋‹ค. ์˜ค๋Š˜์€ ์˜์กด์„ฑ ์ฃผ์ž…์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ์˜์กด์„ฑ์ด ๋ฌด์—‡์ธ์ง€์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜์กด์„ฑ์ด๋ž€? ์œ„ ์ฝ”๋“œ์™€ ๊ฐ™์ด Programmer ๊ตฌ์กฐ์ฒด์—์„œ startProgramming ํ•จ

kiljh.tistory.com