Skip to content

Latest commit

ย 

History

History
113 lines (76 loc) ยท 4.2 KB

Protocol-Oriented-Programming.md

File metadata and controls

113 lines (76 loc) ยท 4.2 KB

Protocol Oriented Programming

Session 408

๐Ÿ”— Protocol-Oriented Programming in Swift - WWDC 2015 - Videos - Apple Developer

OOP๋ฅผ ๊ทธ์ผฌํ•˜๋Š” Crusty๋ฅผ ์„ค๋“์‹œํ‚ค๋‹ค ๋ณธ์ธ์ด ์„ค๋“ ๋‹นํ•˜๋Š” ๋‚ด์šฉ

OOP๊ฐ€ ์งฑ์ด์•ผ ํด๋ž˜์Šค๊ฐ€ ์งฑ์ด์•ผ

  • ๊ด€๋ จ ๋ฐ์ดํ„ฐ๋“ค๊ณผ ์—ฐ์‚ฐ์ž๋“ค์„ ๊ทธ๋ฃน์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค โ†’ ์บก์Šํ™”
  • ์™ธ๋ถ€๋กœ๋ถ€ํ„ฐ ์ฝ”๋“œ ๋‚ด๋ถ€์— ๊ฐ๊ฐ์˜ ๋ฒฝ์„ ์„ธ์šธ ์ˆ˜ ์žˆ๋‹ค โ†’ ์ ‘๊ทผ ์ œ์–ด
  • ์œˆ๋„์šฐ, ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ์ฑ„๋„ ๊ฐ™์€ ๊ด€๋ จ ์•„์ด๋””์–ด๋ฅผ ๋Œ€ํ‘œ ํ•  ์ˆ˜ ์žˆ๋‹ค โ†’ ์ถ”์ƒํ™”
  • ์ถฉ๋Œ์„ ๋ฐฉ์ง€ํ•ด์ฃผ๋Š” ๋„ค์ž„์ŠคํŽ˜์ด์Šค๋ฅผ ์ œ๊ณต ํ•ด ์ค€๋‹ค
  • ์–ด๋ฉ”์ด์ง•ํ•œ ํ‘œํ˜„ ๋ฌธ๋ฒ•๋“ค์ด ์žˆ๋‹ค
  • ํ™•์žฅ์„ฑ์ด ์žˆ๋‹ค

์•„๋‹ˆ์•ผ Type์ด ์งฑ์ด์•ผ ์ด๊ฑฐ ๋‹ค Struct๋ž‘ enum์œผ๋กœ ํ•  ์ˆ˜ ์žˆ์–ด

๋งˆ์ž Swift ์—์„œ ์ด๋ฆ„์„ ๋ถ™์ผ ์ˆ˜ ์žˆ๋Š” ์–ด๋–ค ํƒ€์ž…์ด๋“  1๊ธ‰ ๊ฐ์ฒด์•ผ(first class citizen) ๊ทธ๋ฆฌ๊ณ  ์ด ๋ชจ๋“  ์žฅ์ ๋“ค์„ ๋‹ค ๊ฐ€์ง€๊ณ  ์žˆ์–ด

๊ผญ ํด๋ž˜์Šค๋งŒ ํ•  ์ˆ˜ ์žˆ๋Š” ํ•ต์‹ฌ ๊ธฐ๋Šฅ์„ ์ƒ๊ฐํ•ด ๋ดค์–ด

  • ์Šˆํผํด๋ž˜์Šค๋Š” ๋ณต์žกํ•œ ๋กœ์ง์˜ ์‹ค์งˆ์ ์ธ ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ ํ•  ์ˆ˜ ์žˆ๊ณ  ์„œ๋ธŒํด๋ž˜์Šค์—์„œ๋Š” ์Šˆํผํด๋ž˜์Šค์— ์˜ํ•ด ๋ชจ๋“  ์ž‘์—…์ด ์™„๋ฃŒ๋œ๋‹ค. ์ƒ์†๋งŒ ํ–ˆ์„ ๋ฟ์ธ๋ฐ~
  • ์ง„์งœ ๋งˆ๋ฒ•์€ ์„œ๋ธŒํด๋ž˜์Šค๊ฐ€ override ํ• ๋•Œ ์ƒ๊ธฐ์ง€. ์ด๋Ÿฐ ์ปค์Šคํ„ฐ๋งˆ์ด์ง• ํ•œ ๊ฒƒ์ด ์ƒ์†ํ•œ ๊ตฌํ˜„๋“ค๊ณผ ํ•จ๊ป˜ ๋†“์ธ๋‹ค. ์ด๊ฒƒ์€ ๋ณต์žกํ•œ ๋กœ์ง๋“ค์„ ์žฌ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐœ๋ฐฉ๋œ ์œ ์—ฐ์„ฑ, ๋‹ค์–‘์„ฑ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•œ๋‹ค

Crusty: ๋‚˜ Struct๋กœ ์ปค์Šคํ„ฐ๋งˆ์ด์ง• ๋งจ๋‚ ํ•ด. ๊ทธ๋ž˜ ํด๋ž˜์Šค ๊ฐ•๋ ฅํ•˜์ง€๋งŒ ๋น„์šฉ์— ๋Œ€ํ•ด ์–˜๊ธฐํ•ด๋ณด์ž

  1. Implicit Sharing

    ๋„ˆ๋ฌด ๋งŽ์€ ๋ณต์‚ฌ๊ฐ€ ์ƒ๊ธด๋‹ค. ์ฝ”๋“œ๋ฅผ ๋Š๋ฆฌ๊ฒŒ ํ•œ๋‹ค. ๋™๊ธฐํ™” ๋ฌธ์ œ๊ฐ€ ์žˆ๊ณ . ์Šค๋ ˆ๋“œ๊ฐ€ ๋ณ€ํ•˜๊ธฐ ์‰ฌ์šด ์ƒํƒœ๋“ค์„ ๊ณต์œ  ํ•˜๊ณ  ์žˆ์–ด์„œ lock์„ ํ•ด์ค˜์•ผ ๋œ๋‹ค. ์ด lock์ด ์ฝ”๋“œ๋ฅผ ๋” ๋Š๋ฆฌ๊ฒŒ ํ•œ๋‹ค. ๋ฐ๋“œ๋ฝ์ด ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค. ๋ณต์žกํ•˜๋‹ค

    ์ด ๋ชจ๋“  ์˜ํ–ฅ๋“ค์ด ๊ฒฐ๊ตญ ๋ฒ„๊ทธ ๊ฐ€ ๋œ๋‹ค.

    โ†’ ํ•˜์ง€๋งŒ! Swift Collection๋“ค์€ ๋ชจ๋‘ Value ํƒ€์ž…์ด๊ธฐ ๋•Œ๋ฌธ์— ์ ์šฉ์ด ๋˜์ง€ ์•Š๋Š”๋‹ค!

  2. Inheritance All Up In Your Business ์ƒ์†์€ ํ•˜๋‚˜๋งŒ ๋ฐ›์„ ์ˆ˜ ์žˆ์–ด. ํด๋ž˜์Šค๋ฅผ ์ •์˜ ํ•  ๋•Œ ๊ฒฐ์ •ํ•ด์•ผ ๋˜์ž–์•„ ๋‚˜์ค‘์— extension ํ•  ์ˆ˜ ์—†์ž–์•„. ์Šˆํผํด๋ž˜์Šค์— ์ €์žฅ ํ”„๋กœํผํ‹ฐ๋“ค์ด ์žˆ๋‹ค๋ฉด ๊ทธ๋ƒฅ ๋ฌด์กฐ๊ฑด ๋ฐ›์•„ ๋“œ๋ ค์•ผํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ดˆ๊ธฐํ™” ํ•ด์ค˜์•ผ ํ•œ๋‹ค. ๋ฉ”์†Œ๋“œ๋“ค์ด ์–ด๋–ค์‹์œผ๋กœ override๋˜๊ณ  ์ฒด์ด๋‹ ๋ ์ง€ ์•Œ ์ˆ˜ ์—†๋‹ค

โ†’ ๊ทธ๋ž˜์„œ Delegate ํŒจํ„ด์„ ์“ด๋‹ค

  1. Lost Type Relationships ํด๋ž˜์Šค๋ฅผ ์ถ”์ƒํ™”๋ฅผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ƒ๊ธฐ๋Š” ๋ฌธ์ œ๋“ค

๊ทธ๋ž˜์„œ ๋” ๋‚˜์€ ์ถ”์ƒํ™” ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด ํ•„์š”ํ•ด โ†’ Swift Protocol

Swift Is a Protocol-Oriented Programming Language

Start with a Protocol

    protocol Ordered {
    	func precedes(other: Self) -> Bool
    }
    
    struct Number : Ordered {
    	var value: Double = 0
    	
    	func precedes(other: Number) -> Bool {
    		return self.value < other.value 
    	}
    }
    func binarySearch<T : Ordered>(sortedKeys: [T], forKey k: T) -> Int { var lo = 0
    	var hi = sortedKeys.count
    
    	while hi > lo {
    		let mid = lo + (hi - lo) / 2
    		if sortedKeys[mid].precedes(k) { lo = mid + 1 } else { hi = mid }
    	}
    	
    	return lo 
    }

Protocol Extensions

    protocol Renderer {
      func moveTo(p: CGPoint)
      func lineTo(p: CGPoint)
      func circleAt(center: CGPoint, radius: CGFloat)
      func arcAt(
        center: CGPoint, radius: CGFloat, startAngle: CGFloat, endAngle: CGFloat)
    }
    
    extension Renderer {
    	func circleAt(center: CGPoint, radius: CGFloat) { ... } 
    	func rectangleAt(edges: CGRect) { ... }
    }

When to Use Classes

  • ์•”์‹œ์  ๊ณต์œ (Implicit Sharing)์ด ํ•„์š”ํ•  ๋•Œ
  • ์‹œ์Šคํ…œ(Cocoa, Cocoa Touch)๋Š” ๊ฐ์ฒด๋ฅผ ๋‹ค๋ฃจ๊ณ , ์Šˆํผํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์„œ๋ธŒํด๋ž˜์‹ฑ์„ ํ•ด์•ผ๋˜๊ฑฐ๋‚˜ ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•ด์•ผ๋ผ.

Summary

  • Protocols > Superclasses
  • Protocol extensions = magic(almost)
  • Be like Crusty