NOW-ON / iOS-Interview-Preparation

iOS ๋ฉด์ ‘ ์ค€๋น„
3 stars 0 forks source link

Delegate Pattern #10

Closed Glsme closed 1 year ago

Glsme commented 1 year ago

๐Ÿ—“๏ธ ๋งˆ๊ฐ์ผ

2023.01.22

๐Ÿ’ ์ฐธ์—ฌ์ž

โš ๏ธ ๋งˆ์ง€๋ง‰์œผ๋กœ ๋‹ต๋ณ€ ๋‹จ ์‚ฌ๋žŒ์ด ์ด์Šˆ close ํ•˜๊ณ  ๋ฆฌ๋“œ๋ฏธ์— ํ•ญ๋ชฉ ์—…๋ฐ์ดํŠธ ํ•ด์ฃผ์„ธ์š”.

Yun-YeoJin commented 1 year ago
//๊ฐ’์„ ๋ณด๋‚ผ SearchViewController
final class SearchViewController: BaseViewController {

lazy var delegate: SendStockDelegate?

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let stockNameData = stockList[indexPath.row].stockName
        delegate?.sendStockName(stockNameData)
        self.dismiss(animated: true)
    }

//๊ฐ’์„ ๋ฐ›์„ BuyandSellViewController
public protocol SendStockDelegate: AnyObject {
    func sendStockName(_ text: String)
}

final class BuyandSellViewController: BaseViewController, SendStockDelegate {
    func sendStockName(_ text: String) {
        mainView.contentSearchBar.text = text
    }
}
LeeJoobang commented 1 year ago
heydoy commented 1 year ago

Delegation

ํ”„๋กœํ† ์ฝœ์„ ์ •์˜ํ•˜์—ฌ ๊ตฌํ˜„ํ•จ์œผ๋กœ์จ ์œ„์ž„์ด ๋œ ์ฑ…์ž„์„ ์บก์Šํ™”ํ•˜๋ฉฐ, ์ค€์ˆ˜ํ•œ ์œ ํ˜•(Delegate=๋Œ€๋ฆฌ์ž)์ด ์œ„์ž„๋œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋„๋ก ๋ณด์žฅํ•œ๋‹ค. Delegation์€ ํŠน์ • ์•ก์…˜์— ๋ฐ˜์‘ํ•˜๋„๋ก ํ•˜๊ฑฐ๋‚˜, ์†Œ์Šค์˜ ๊ธฐ๋ณธํƒ€์ž…์„ ์•Œ์ง€ ๋ชป ํ•ด๋„ ์™ธ๋ถ€์— ์žˆ๋Š” ์†Œ์Šค์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ๋“ฑ์— ์‚ฌ์šฉ๋œ๋‹ค.

๋ชฉ์ 

Delegate ํŒจํ„ด์˜ ํ•ต์‹ฌ์ ์ธ ๋ชฉํ‘œ๋Š” ๊ฐ์ฒด๊ฐ€ ๋ถ„๋ฆฌ๋œ ๋ฐฉ์‹์œผ๋กœ ๊ฐ์ฒด ์†Œ์œ ์ž์™€ ํ†ต์‹ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ฐ์ฒด๊ฐ€ ์†Œ์œ ์ž์˜ ๊ตฌ์ฒด์ ์ธ ์œ ํ˜•์„ ์•Œ์ง€ ์•Š๋„๋ก ํ•จ์œผ๋กœ์จ, ๊ฐœ๋ฐœ์ž๊ฐ€ ์žฌ์‚ฌ์šฉ ๋ฐ ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ๋” ์‰ฌ์šด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค.

์˜ˆ์‹œ์ฝ”๋“œ

// ์ด ํ”„๋กœํ† ์ฝœ์€ ๊ณ ์–‘์ด๊ฐ€ ์ง‘์‚ฌ์—๊ฒŒ ์ง€์‹œํ•  ๋ชจ๋“  ๊ฒƒ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Œ 
protocol CatDelegate {
    func feedCat(for cat: Cat, food: CatFood) -> CatFood
    func howMuchTimeWePlayed(for cat: Cat, toy: Toy) -> Int
    func isSandPreferable() -> Bool
}

class Cat {
    // ๊ณ ์–‘์ด๋Š” ์ง‘์‚ฌ๊ฐ€ ๋ฌด์Šจ ํด๋ž˜์Šค์ด๋“  ์ƒ๊ด€์—†์Œ 
    // ๋‹จ์ง€ ์ง‘์‚ฌ๊ฐ€ ๊ณ ์–‘์ด๋ฅผ ์œ„ํ•ด ํ•ด์•ผ๋˜๋Š” ์ผ์„ ํ•  ์ˆ˜ ์žˆ์–ด์•ผํ•จ.
    weak var jipsa: CatDelegate? 

    // ์ง‘์‚ฌ๊ฐ€ ์ผ์„ ํ•˜๋Š” ํ‰์ผ๊ณผ  whenever the manager needs those tasks done, they just have the assistant do it
    func performTasks {
      switch today {
         case .weekday: 
              catFood = jipsa.feedCat(for: self, catFood: Tuna) 
              playTime = jipsa.howMuchTimeWePlayed(for: self, toy: KashaKasha) 
         case .weekend:
             isSandPreferable = jipsa.isSandPreferable()
     }
}
// ์ง‘์‚ฌ๋Š” ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•ด์•ผํ•œ๋‹ค. ํ”„๋กœํ† ์ฝœ์˜ ์š”๊ตฌ์‚ฌํ•ญ์„ ๋ชจ๋‘ ๋งŒ์กฑํ•ด์•ผ ์ปดํŒŒ์ผ์ด ๋œ๋‹ค. 
class Jipsa: CatDelegate {
    func feedCat(for cat: Cat, food: CatFood) -> CatFood {
       // ๊ณ ์–‘์ด์—๊ฒŒ ๋ฐฅ์„ ์ฃผ๋Š” ์ฝ”๋“œ 
       return food 
    }
    func howMuchTimeWePlayed(for cat: Cat, toy: Toy) -> Int {
      // ๊ณ ์–‘์ด์™€ ์žฅ๋‚œ๊ฐ์œผ๋กœ ๋†€์•„์ฃผ๊ธฐ  
      return time
   }
    func isSandPreferable() -> Bool  {
       // ๊ณ ์–‘์ด ๋ชจ๋ž˜ ์„ ํ˜ธ๋„ ๋ถ„์„ํ•˜๊ธฐ
      return true
    }
}

์™œ Delegate Pattern์„ ์“ธ๊นŒ?

  1. ์ ์ ˆํ•˜๊ฒŒ ๊ด€์‹ฌ์‚ฌ๋ฅผ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ -> ํด๋ž˜์Šค ๊ฐ„ ๊ฒฐํ•ฉ๋„๋ฅผ ๋‚ฎ์ถค (decoupling) ใ„ด ์˜ˆ๋ฅผ ๋“ค์–ด, ๊ณ ์–‘์ด๋Š” ๊ณ ์–‘์ด์— ๋Œ€ํ•œ ๋…ผ๋ฆฌ๋งŒ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๊ณ , ๊ณ ์–‘์ด๋ฅผ ์ง€์›ํ•ด์•ผ๋˜๋Š” ์ผ์— ๋Œ€ํ•ด์„œ ํด๋ž˜์Šค์— ์ •์˜ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
  2. ์žฌ์‚ฌ์šฉ์„ฑ
Glsme commented 1 year ago

Delegate

๋””์ž์ธ ํŒจํ„ด์œผ๋กœ ํด๋ž˜์Šค๋‚˜ ๊ตฌ์กฐ์ฒด์˜ ์ฑ…์ž„ ์ค‘ ์ผ๋ถ€๋ถ„์„ ๋‹ค๋ฅธ ํƒ€์ž…์˜ ์ธ์Šคํ„ด์Šค์—๊ฒŒ ์ „๋‹ฌ(์œ„์ž„)ํ•˜๋Š” ์—ญํ• ์„ ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.



Delegate Pattern

์œ„์ž„์ž๋ฅผ ๊ฐ–๊ณ  ์žˆ๋Š” ๊ฐ์ฒด๊ฐ€ ๋‹ค๋ฅธ ๊ฐ์ฒด์—๊ฒŒ ์ž์‹ ์˜ ์ผ์„ ์œ„์ž„ํ•˜๋Š” ํ˜•ํƒœ์˜ ๋””์ž์ธ ํŒจํ„ด์ด๋‹ค.

Delegate Pattern ๋™์ž‘ ๋ฐฉ์‹

  1. Delegate Protocol์„ ์„ ์–ธํ•œ๋‹ค.
  2. ์ผ์„ ์ „๋‹ฌํ•˜๋Š” ViewController ๋‚ด ํ”„๋กœํผํ‹ฐ์— Delegate๋ฅผ ์ €์žฅํ•œ๋‹ค.
  3. ์ผ์„ ์ „๋‹ฌ๋ฐ›์„ ViewController์—์„œ ์ „๋‹ฌํ•˜๋Š” ViewController ๋‚ด Delegate๋ฅผ ์ €์žฅํ•œ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ž์‹ ์œผ๋กœ ์œ„์ž„์ž๋กœ ์‚ผ๋Š”๋‹ค.
  4. ์ผ์„ ์ „๋‹ฌ๋ฐ›์„ ViewController์—์„œ ์ผ์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.



Delegate Pattern์„ ์‚ฌ์šฉํ•˜๋ฉด retain๋˜๋Š” ์ด์œ 

Delegate Pattern์„ ์‚ฌ์šฉํ•˜๋ฉด ๋‘๊ฐœ์˜ ํด๋ž˜์Šค๊ฐ€ ์„œ๋กœ ์ฐธ์กฐํ•˜๋Š” ์ƒํ™ฉ์ด ํ•„์—ฐ์ ์œผ๋กœ ๋ฐœ์ƒํ•œ๋‹ค. ์ด ๋•Œ ์ผ์„ ์ „๋‹ฌ๋ฐ›์„ ViewController์—์„œ Delegate๋ฅผ ์ฒ˜๋ฆฌ ํ›„ ์ผ์„ ์ „๋‹ฌํ•˜๋Š” ViewController๋Š” ์ด๋ฏธ deinit์ด ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ผ์„ ์ „๋‹ฌ๋ฐ›์€ ์ž…์žฅ์—์„  ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ•ด์ œํ•  ์ˆ˜ ์—†๋‹ค. ๋”ฐ๋ผ์„œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ •์ƒ์ ์œผ๋กœ ํ•ด์ œํ•˜๋ ค๋ฉด weakํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์—ฌ MemoryLeak์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค.

wodyddml2 commented 1 year ago

Delegate Pattern

์œ„์ž„์ž(Delegate)๋ฅผ ๊ฐ–๊ณ  ์žˆ๋Š” ๊ฐ์ฒด๊ฐ€ ๋‹ค๋ฅธ ๊ฐ์ฒด์—๊ฒŒ ์ž์‹ ์˜ ์ผ์„ย ์œ„์ž„ํ•˜๋Š” ํ˜•ํƒœ์˜ ๋””์ž์ธ ํŒจํ„ด

Delegate Pattern ์‚ฌ์šฉ ์‹œ ์ฃผ์˜ํ•  ์ 

๊ฐ์ฒด๋ผ๋ฆฌ ์—ฐ๊ฒฐํ•˜๋ฉด์„œ ๊ฐ•ํ•œ ์ฐธ์กฐ ์ˆœํ™˜์ด ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— weak ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋ฉ”๋ชจ๋ฆฌ ๋ฆญ์„ ๋ฐฉ์ง€ํ•ด์•ผํ•œ๋‹ค.