ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐ˜์‘ํ˜•

ios-og


๐Ÿ™‹ protocol์— ๋ฉ”์†Œ๋“œ ์—†์ด extension์—์„œ ๋ฉ”์†Œ๋“œ ๊ตฌํ˜„ํ•˜๊ธฐ vs protocol์— ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ ํ›„ extension์— ๊ตฌํ˜„ํ•˜๊ธฐ


๐ŸŽฏ ๊ฐœ๋… ์ดํ•ดํ•˜๊ธฐ


  • protocol default implementation์„ ํ•˜๊ธฐ ์œ„ํ•ด protocol์˜ extension์„ ํ†ตํ•ด ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋‹ค๊ฐ€ ๋ณด๋ฉด ๋‘๊ฐ€์ง€ ์˜ต์…˜์ด ์ƒ๊น๋‹ˆ๋‹ค. ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ๋ฌธ์ œ๋ฅผ ์ •์˜ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

    protocol Bank {
      // ์ด ๋ถ€๋ถ„ ์—†์ด ๊ตฌํ˜„ ๊ฐ€๋Šฅ
      func openAlert()
      func closeAlert()
      //
    }
    
    extension Bank {
      func openAlert() {
        print("์€ํ–‰ ๊ฐœ์ ")
      }
    
      func closeAlert() {
        print("์€ํ–‰ ํ์ ")
      }
    }

    ์€ํ–‰์˜ ๊ฐœ์ ๊ณผ ํ์ ์„ ์•Œ๋ฆฌ๋Š” ๋ฉ”์†Œ๋“œ openAlert์™€ closeAlert๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ

    1. protocol Bank์— ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ•˜๊ณ  ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•
    2. protocol Bank์— ๋ฉ”์†Œ๋“œ ์ •์˜ ์—†์ด extension์—์„œ ๊ตฌํ˜„๋งŒ ํ•˜๋Š” ๋ฐฉ๋ฒ•

    ์˜ ์˜ต์…˜์ด ์žˆ์Šต๋‹ˆ๋‹ค.


    ์šฐ์„  1๋ฒˆ์— ๋Œ€ํ•œ ์„ค๋ช…์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

    Declaring the function as part of the protocol definition instructs the compiler to use dynamic dispatch when calling the function, as the compiler would expect types implementing the protocol to give an implementation for that function. This is called a method requirement. Now, if the type doesn't define the method, then the runtime resolves the method call to the method declared in the protocol extension.

    ํ•ด์„ํ•ด๋ณด๋ฉด ํ”„๋กœํ† ์ฝœ์— ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๋ฉด ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ ์ปดํŒŒ์ผ๋Ÿฌ์—๊ฒŒ dynamic dispatch (ํ•˜๋‹จ์— ์„ค๋ช…) ๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ํ”„๋กœํ† ์ฝœ์„ ๊ตฌํ˜„ํ•˜๋Š” ํƒ€์ž…์ด ๊ทธ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด๋†จ๊ธฐ๋ฅผ ๊ธฐ๋Œ€ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์„ method requirement๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋งŒ์•ฝ ๋ฉ”์†Œ๋“œ๊ฐ€ ํƒ€์ž…์— ๊ตฌํ˜„์ด ๋˜์–ด์žˆ์ง€ ์•Š์œผ๋ฉด, ๋Ÿฐํƒ€์ž„์ด ํ”„๋กœํ† ์ฝœ ์ต์Šคํ…์…˜์— ๊ตฌํ˜„๋œ ๋ฉ”์†Œ๋“œ๋ฅผ ๋ถ€๋ฅด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๊ตฌํ˜„๋œ ํƒ€์ž…์˜ ๋ฉ”์†Œ๋“œ์ด๋ฉฐ, ๋ฉ”์†Œ๋“œ๊ฐ€ ๊ตฌํ˜„๋˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ extension์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฐœ๋…์ž…๋‹ˆ๋‹ค.


    ์ด์ œ 2๋ฒˆ์— ๋Œ€ํ•œ ์„ค๋ช…์„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

    However, declaring the function in the protocol extension only tells the compiler that he doesn't need to use the dynamic dispatch, and instead it uses the static dispatch, which is faster, but doesn't work very well with polymorphism, as the protocol extension implementation will be called even if the types conforming to the protocol also implement the method.

    protocol ์ƒ์—๋Š” ์—†๊ณ  extension์—๋งŒ ๊ตฌํ˜„๋œ ๋ฉ”์†Œ๋“œ์˜ ๊ฒฝ์šฐ๋Š” ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ dynamic dispatch๋ฅผ ์‚ฌ์šฉํ•  ์ด์œ ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๋Œ€์‹ ์— static dispatch๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ์ด ๊ฒฝ์šฐ protocol์„ ์ฑ„ํƒํ•˜๋Š” ํƒ€์ž…์— ๋™์ผํ•œ ๋ฉ”์†Œ๋“œ๊ฐ€ ๊ตฌํ˜„๋˜์–ด ์žˆ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ protocol extension์— ์กด์žฌํ•˜๋Š” ๋ฉ”์†Œ๋“œ๊ฐ€ ์šฐ์„ ์ ์œผ๋กœ ํ˜ธ์ถœ๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.


๐Ÿ“ ์˜ˆ์‹œ๋กœ ์ดํ•ดํ•˜๊ธฐ


  • ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ์œ„์˜ ๊ฐœ๋…์„ ์ดํ•ดํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

    protocol Fruit {
        func draw()
    }
    
    extension Fruit {
        func draw(){
            print("This is a Fruit")
        }
    }
    
    struct Apple: Fruit {
        func draw() {
            print("This is an Apple")
        }
    }
    
    struct Orange: Fruit {
        func draw() {
            print("This is an Orange")
        }
    }
    
    let fruits: [Fruit] = [Apple(), Orange()]
    
    for fruit in fruits {
        fruit.draw()
    }

    ๊ณผ์ผ์„ ๊ทธ๋ ค๋ณด๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ตฌ์กฐ๋ฅผ ์‚ดํŽด๋ณด๋ฉด ๊ฐœ๋… ์„ค๋ช… 1์— ํ•ด๋‹นํ•˜๋Š” ํ”„๋กœํ† ์ฝœ ๋‚ด์— ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๊ณ  extension์— ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•œ ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ compiler๊ฐ€ protocol์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ๋ณด๋Š” ์ˆœ๊ฐ„ dynamic dispatch๋ฅผ ์ ์šฉํ•˜์—ฌ Fruit protocol์„ conformํ•˜๋Š” Apple, Orange ์•ˆ์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์šฐ์„ ์ ์œผ๋กœ ํ˜ธ์ถœํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š”

    This is an Apple

    This is an Orange

    ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

    ์ด๋ฒˆ์—๋Š” Fruit protocol์„ ๋น„์›Œ์„œ ์ •์˜ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

    protocol Fruit {
       // ๋ฉ”์†Œ๋“œ ์—†์Œ
    }
    ...

    ์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ๋ชจ๋‘ ๋™์ผํ•˜๋‹ค๊ณ  ํ•  ๋•Œ, ์ด๋ฒˆ์—๋Š” ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ static dispatch๋กœ ์ž‘๋™ํ•˜์—ฌ extension Fruit๋ฅผ ๋จผ์ € ์ฒดํฌํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ๋ฉ”์†Œ๋“œ๊ฐ€ ์กด์žฌํ•˜๊ณ , ์ด ๋ฉ”์†Œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜์–ด Fruit๋ฅผ ์ฑ„ํƒํ•˜๋Š” Apple๊ณผ Orange์— ์กด์žฌํ•˜๋Š” ๋™์ผ ์ด๋ฆ„์˜ ๋ฉ”์†Œ๋“œ๋Š” ํ˜ธ์ถœ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๊ฒฐ๊ณผ๋Š”

    This is a Fruit

    This is a Fruit

    ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.


๐Ÿ‘ ๊ฒฐ๋ก 

  • ๊ฒฐ๊ณผ์ ์œผ๋กœ ์ •๋ฆฌํ•˜๋ฉด protocol์— ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ€์ง„ ์ƒํƒœ์—์„œ extension์— ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ๊ณผ, extension์—๋งŒ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์€ ๊ทธ ๋ฉ”์†Œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๋Š” ์šฐ์„ ์ˆœ์œ„์—์„œ ์ฐจ์ด๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. protocol์„ ์ฑ„ํƒํ•˜๋Š” ํƒ€์ž…์ด ๊ฐ€์ง„ ๋ฉ”์†Œ๋“œ๋ฅผ ์šฐ์„ ์ ์œผ๋กœ ํ˜ธ์ถœํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด protocol์ด ๊ฐ€์ ธ์•ผํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์„ค์ •ํ•ด์„œ, compiler๊ฐ€ ๊ทธ ํ”„๋กœํ† ์ฝœ์„ ๋ณผ ๋•Œ ๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•œ ๋ฉ”์†Œ๋“œ๋ฅผ ์ธ์‹์‹œ์ผœ ํƒ€์ž…์— ์กด์žฌํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ํ•˜๊ณ , ๊ทธ ๋ฐ˜๋Œ€์˜ ๊ฒฝ์šฐ๋Š” extension์—๋งŒ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๐Ÿ“ธ Dispatch


  • dispatch๋ž€ ํ”„๋กœ๊ทธ๋žจ์ด ์–ด๋–ค ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๊ฒƒ์ธ๊ฐ€๋ฅผ ๊ฒฐ์ •ํ•˜์—ฌ ๊ทธ๊ฒƒ์„ ์‹คํ–‰ํ•˜๋Š” ๊ณผ์ • ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. dispatch๋Š” ํฌ๊ฒŒ ๋‘๊ฐ€์ง€๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    1. Static Dispatch(์ •์  ๋””์ŠคํŒจ์น˜)

      ์ปดํŒŒ์ผ ์‹œ์ ์—์„œ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ํŠน์ • ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๊ฒƒ์ด๋ผ๋Š” ๊ฑธ ์ •ํ™•ํ•˜๊ฒŒ ์•Œ๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ ๋Ÿฐํƒ€์ž„(์‹คํ–‰ ์‹œ์ )์ด ๋˜์ง€ ์•Š์•„๋„ ๋ฏธ๋ฆฌ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์ด๊ฒƒ์„ ์ •์  ๋””์ŠคํŒจ์น˜๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

    2. Dynamic Dispatch(๋™์  ๋””์ŠคํŒจ์น˜)

      ํŠน์ • ๋ฉ”์†Œ๋“œ๊ฐ€ ์ปดํŒŒ์ผ ์‹œ์ ์ด ์•„๋‹ˆ๋ผ ๋Ÿฐํƒ€์ž„ ์‹œ์ ์— ํ˜ธ์ถœ์ด ๊ฒฐ์ •๋  ๊ฒฝ์šฐ Dynamic Dispatch๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.


ref: https://stackoverflow.com/questions/34777926/reasons-to-include-function-in-protocol-definition-vs-only-defining-it-in-the-e

๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๋ฐ˜์‘ํ˜•
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG more
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
๊ธ€ ๋ณด๊ด€ํ•จ