Skip to content

Latest commit

ย 

History

History
282 lines (153 loc) ยท 10.7 KB

Protocol-Oriented-Programming-in-Swift.md

File metadata and controls

282 lines (153 loc) ยท 10.7 KB

Protocol Oriented Programming in Swift

Table of contents

  1. Class(OOP) VS Struct, Enum(POP)
  2. The Three Beefs
  3. Swift is a POP Language
  4. More Protocol Extension Tricks
  5. They do have their place...

Class(OOP) VS Struct, Enum(POP)

  • Encapsulation: ๊ทธ๋ฃนํ™”
  • Access Control: ์ ‘๊ทผ ์ œ์–ด์ž
  • Abstraction: ์ถ”์ƒํ™”
  • Namespace: software๊ฐ€ ์ปค์ง€๋ฉด์„œ ๋ฐœ์ƒํ•˜๋Š” ์ถฉ๋Œ์„ ๋ง‰์•„์ค€๋‹ค
  • Expressive Syntax: ์ €์žฅ/์—ฐ์‚ฐ ํ”„๋กœํผํ‹ฐ, ๋ฉ”์†Œ๋“œ, ์„œ๋ธŒ์Šคํฌ๋ฆฝ์…˜..
  • Extensibility: ๋‚˜์ค‘์— ํ•„์š”ํ•œ๊ฑฐ ์ถ”๊ฐ€ ๊ฐ€๋Šฅ

Access Control, Abstraction, Namespace๋Š” ํŠนํžˆ ๋ณต์žก์„ฑ(Complexity)์„ ๊ด€๋ฆฌํ•˜๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค.

  • struct, enum๋„ ํ•  ์ˆ˜ ์žˆ์Œ.
  • Swift์—์„œ ์ด๋ฆ„ ์ง€์„ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  type์€ ์ผ๊ธ‰๊ฐ์ฒด์ด๊ณ  ์œ„์— ๋ชจ๋“  ํŠน์ง•์„ ๊ฐ–์„ ์ˆ˜ ์žˆ๋‹ค.

  • ์ƒ์†์€ class๋งŒ ๊ฐ€๋Šฅ

  • ์ƒ์†์„ ํ†ตํ•ด ์ฝ”๋“œ๊ณต์œ , ์ปค์Šคํ…€ ๊ฐ€๋Šฅ(override)

  • struct๋„ ์ปค์Šคํ…€ ๊ฐ€๋Šฅ

  • class๋Š” ๊ฐ•๋ ฅํ•˜์ง€๋งŒ cost๊ฐ€ ํฌ๋‹ค


The Three Beefs

1. Implicit Sharing

  • ๊ฐ’ ํƒ€์ž…์€ ์ฐธ์กฐ๋ฅผ ๊ณต์œ ํ•ด ์˜๋„ํ•˜์ง€ ์•Š์€ ๋ณ€ํ™”๊ฐ€ ๋ฐœ์ƒํ•ด ๋ฌธ์ œ๋  ์ˆ˜ ์žˆ๋‹ค.

  • ๊ฐ’ ํƒ€์ž…์€ cost๊ฐ€ ํฌ๋‹ค

2. Inheritance All Up In Your Business

  • Superclass 1๊ฐœ๋งŒ ๊ฐ–์„ ์ˆ˜ ์žˆ๋‹ค.
  • ํ•„์š” ์—†๋Š”๊ฒƒ๋„ ๋‹ค ์ƒ์† ๋ฐ›์Œ
  • class๋ฅผ ์ •์˜ํ•˜๋Š” ์ˆœ๊ฐ„ super class๋ฅผ ์ •ํ•ด์•ผํ•œ๋‹ค.(extension์œผ๋กœ ์ƒ์† ๋ถˆ๊ฐ€๋Šฅ)
  • Superclass์— ์ €์žฅ ํ”„๋กœํผํ‹ฐ๊ฐ€ ์žˆ๋‹ค๋ฉด
    • ์ƒ์† ๋ฐ›์•„์•ผ๋งŒ ํ•œ๋‹ค.
    • ์ €์žฅํ”„๋กœํผํ‹ฐ๋“ค์„ ์ดˆ๊ธฐํ™” ํ•ด์ค˜์•ผํ•œ๋‹ค.
    • superclass์˜ invariant๋ฅผ ๊นจ๋ฉด์•ˆ๋œ๋‹ค.
  • method๋“ค์ด override๋  ๊ฑด๊ฐ€ ์•Œ์•„์•ผํ•œ๋‹ค.(final ํ‚ค์›Œ๋“œ ๋ช…์‹œ)
  • cocoa์—์„œ delegate ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ์ž…๋‹ˆ๋‹ค.

3. Lost Type Relationships

  • Ordered์˜ precedes์˜ ๊ตฌํ˜„๋ถ€์— ์–ด์ฉ”์ˆ˜ ์—†์ด ๋ถˆ์•ˆํ•œ ๊ตฌํ˜„
  • Number์˜ precede์˜ other์€ Label์ด ๋  ์ˆ˜ ๋„ ์žˆ๋‹ค.(static type safety hole)

  • as! ASubclass์€ code smell ์ด๋‹ค.
  • as! ASubclass์€ type relationship์ด ์†์ƒ๋์Œ์„ ์˜๋ฏธํ•œ๋‹ค.
  • Swift๋Š” ์ •์  ํƒ€์ž… ์–ธ์–ด๋กœ ๋ณ€์ˆ˜, ์ƒ์ˆ˜์˜ ํƒ€์ž…์ด ์ปดํŒŒ์ผ ์‹œ์ ์— ๊ฒฐ์ •๋˜๋Š”๋ฐ ํƒ€์ž… ์บ์ŠคํŒ…์„ ์‚ฌ์šฉํ•˜๋ฉด ๋Ÿฐํƒ€์ž„์— ํƒ€์ž…์ด ์ •ํ•ด์ ธ ์บ์ŠคํŒ…์— ์‹คํŒจํ•˜๋Š” ๊ฒฝ์šฐ ๋Ÿฐํƒ€์ž„์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

  • protocol๋กœ ํ•˜๋ฉด๋จ

Swift Is a Protocol-Oriented Programming Language


Start with a Protocol

Your first stop for new abstractions

  • Ordered์˜ ๋ถˆํ•„์š”ํ•œ ๊ตฌํ˜„๋ถ€ ์‚ญ์ œ(fatal error ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š์•„๋„ ๋จ)

  • override ์—ฌ๋ถ€๋ฅผ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š์•„๋„ ๋จ
  • ์ƒ์†์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ์œผ๋กœ class -> struct๋กœ ๋ณ€๊ฒฝ

  • ๊ฐ•์ œ ํƒ€์ž…์บ์ŠคํŒ…(static type safety hole) ์ œ๊ฑฐ
  • Self๋Š” ์ž๊ธฐ ์ž์‹ ์˜ ํƒ€์ž…์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.

Using Our Protocol

  • Ordered๋Š” Label์ด ๋  ์ˆ˜ ๋„์žˆ๊ณ  Number๊ฐ€ ๋ ์ˆ˜๋„์žˆ๋Š” heterogeneous์ธ array๋ผ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ Homogeneous์ธ array๋กœ ๋ฐ”๊พธ๋ผ๊ณ ํ•œ๋‹ค.

  • generic์„ ์‚ฌ์šฉํ•˜๋ฉด Homogeneous์ธ array๋กœ ์ธ์‹์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.


Protocols and Generics for Testability

  • mocks์€ ํ…Œ์ŠคํŒ… ์ฝ”๋“œ๋ฅผ ์„ธ๋ถ€ ๊ตฌํ˜„๊ณผ ์—ฐ๊ฒฐํ•ด์•ผ ํ•˜์ง€๋งŒ ํ”„๋กœํ† ์ฝœ์€ ์—ฐ๊ฒฐ์„ ๋Š์„ ์ˆ˜ ์žˆ์–ด ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์šฉ์ดํ•ฉ๋‹ˆ๋‹ค.

  • protocol์€ ์šฐ๋ฆฌ์—๊ฒŒ ์–ธ์–ด์— ์˜ํ•ด ๊ฐ•์š”๋˜๋Š” ์›์น™์ ์ธ interface๋ฅผ ์ œ๊ณตํ•˜๊ธฐ๋„ ํ•˜๊ณ  ์šฐ๋ฆฌ๊ฐ€ ํ•„์š”ํ•œ ๋„๊ตฌ๋“ค์„ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” hook์„ ์ œ๊ณตํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.

  • extension์„ ์‚ฌ์šฉํ•ด ๋ฐ˜๋ณต๋˜๋Š” ๊ตฌํ˜„์„ ํ•œ๋ฒˆ๋งŒ ๊ตฌํ˜„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

  • circleAt์€ ์š”๊ตฌ์‚ฌํ•ญ์— ์žˆ๊ณ , extension์— ๊ตฌํ˜„
  • rectangleAt์€ ์š”๊ตฌ์‚ฌํ•ญ์— ์—†๊ณ , extension์— ๊ตฌํ˜„

  • type inference์„ ์‹œํ‚ฌ ๊ฒฝ์šฐ์—๋Š” ๋‘˜๋‹ค ์ƒˆ๋กœ ์ปค์Šคํ…€ํ•œ๊ฒŒ ์‹คํ–‰๋œ๋‹ค.

  • type annotation ํ•˜๋ฉด ์š”๊ตฌ์ƒํ•ญ์— ์—†๋Š” retangleAt์€ ์ปค์Šคํ…€ํ•œ ๋ถ€๋ถ„์ด ์•„๋‹ˆ๋ผ protol์˜ extension์— ๊ตฌํ˜„ํ•œ ๋ถ€๋ถ„์ด ์‹คํ–‰๋œ๋‹ค.

More Protocol Extension Tricks

Scenes from the standard library and beyond

Constrained extensions

  • ์–ด๋–ค collection์˜ ์š”์†Œ๋“ค์€ == ๋กœ ๋น„๊ตํ•  ์ˆ˜ ์—†๋‹ค.

  • where์„ ์‚ฌ์šฉํ•ด Equatable Protocol์„ ๋”ฐ๋ฅด๋Š” element๋“ค๋งŒ ์—ฐ์‚ฐํ•  ์ˆ˜ ์žˆ๊ฒŒ ์ˆ˜์ •

Retroactive adaption

  • Int๋Š” Ordered ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•˜์ง€ ์•Š์•„ ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€๋Šฅํ•จ

  • Int, String ์ด Ordered ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•˜๊ฒŒํ•ด ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•จ

  • Comparable ํ”„๋กœํ† ์ฝœ์—๋Š” '<' ์—ฐ์‚ฐ์ž๊ฐ€ ์ด๋ฏธ ์กดํ•ดํ•œ๋‹ค.
  • Comparable์—์„œ extension์„ ํ†ตํ•ด precedes๋ฅผ ๊ตฌํ˜„ํ•ด ๊ฐœ์„ 

  • Comparable์„ ํ™•์žฅํ•ด Double ํƒ€์ž…์ด precedes ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ Ordered ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•˜์ง€ ์•Š์•„ Double ํƒ€์ž…์—์„œ binarySearch ๋ฉ”์†Œ๋“œ๋Š” ์‚ฌ์šฉ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค

  • constrained extension ์‚ฌ์šฉํ•ด Ordered ํ™•์žฅํ•ด Ordered๋ฅผ ์ฑ„ํƒํ•œ ํƒ€์ž…๋งŒ precedes ๋ฉ”์†Œ๋“œ์™€ binarySearch ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•จ

Generic beautification

  • Swift1 ์˜ fully generallized binary search

  • Swift2 ์—์„œ Protocol์„ ์‚ฌ์šฉํ•ด ๊ฐœ์„ 

Interface generation

  • Protocol์„ ์ฑ„ํƒํ•˜๋ฉด extension์— ๊ตฌํ˜„๋œ ์ธํ„ฐํŽ˜์ด์Šค๋“ค ์‚ฌ์šฉ๊ฐ€๋Šฅ

Building bridges between the static and dynamic worlds

  • drawable ๋ฐฐ์—ด์„ ๋น„๊ตํ•  ์ˆ˜ ์—†๋‹ค.

  • ๋ฐฐ์—ด์˜ count ๊ฐ€ ๊ฐ™๊ณ  ํ•˜๋‚˜์”ฉ ๋น„๊ตํ•ด ๊ฐ™์€์ง€ ํ™•์ธ
  • Drawable์€ Equatable ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•˜์ง€ ์•Š์•„์„œ == ์—ฐ์‚ฐ์ž๋กœ ๋น„๊ตํ•  ์ˆ˜ ์—†๋‹ค.

  • Equatable ํ”„๋กœํ† ์ฝœ์„ Self๋ฅผ ์‚ฌ์šฉํ•ด ๋น„๊ต(Static Dispatch, Homogeneous)ํ•˜๋Š”๋ฐ Drawable์€ Circle์ด ๋ ์ˆ˜๋„ Polygon์ด ๋ ์ˆ˜๋„ ์žˆ์–ด(Dynamic Dispatch, Heterogeneous)์„œ ์ถฉ๋Œํ•œ๋‹ค.

  • isEqualTo ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋น„๊ตํ•œ๋‹ค.

  • extension์„ ํ†ตํ•ด ๋‹ค์šด ์บ์ŠคํŒ…์„ ํ†ตํ•ด์„œ type์„ ๋ณ€ํ˜•ํ•˜๋ฉด Equatable์„ ์ฑ„ํƒํ•˜๊ณ  Self๋ฅผ ์‚ฌ์šฉํ•ด ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋‹ค.
  • Static Dispatch -> Dynamic Dispatch๋กœ ๋ณ€๊ฒฝ
  • Heterogeneous -> Homogeneous๋กœ ๋ณ€๊ฒฝ

They(Classes) do have their place...

implicitํ•œ ๊ณต์œ ๊ฐ€ ํ•„์š”ํ•  ๋•Œ

  • ๋ณต์‚ฌ๋‚˜ ๋น„๊ต๊ฐ€ ์˜๋ฏธ์—†๋Š”์ƒํƒœ(Singleton ์ฒ˜๋Ÿผ ํ•œ๋ฒˆ ๋งŒ๋“ค์–ด ๋†“๊ณ  ์“ฐ๋Š”๊ฑฐ ๋งํ•˜๋Š”๊ฑฐ ๊ฐ™์Œ)(e.g., Window)
  • ์ธ์Šคํ„ด์Šค์˜ ์ƒ๋ช…์ฃผ๊ธฐ๊ฐ€ ์™ธ๋ถ€์š”์ธ๊ณผ ๊ด€๋ จ์žˆ์„๋•Œ(e.g., TemporaryFile)
    • reference type์€ ์•ˆ์ „ํ•œ identity๊ฐ€ ์žˆ์–ด ์™ธ๋ถ€์˜ entity์™€ correspondํ•˜๋Š” ๋จผ๊ฐ€๋ฅผ ๋งŒ๋“ ๋‹ค๋ฉด ์ฐธ์กฐ ํƒ€์ž…์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
  • ์ธ์Šคํ„ด์Šค๋Š” ๋ฐ์ดํ„ฐ์˜ ๊ตฌ์กฐ๋ฅผ ์ •์˜ํ•˜๊ณ  ์ƒํƒœ๋ฅผ ์ €์žฅํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๊ณ  ๋ฉ”์„œ๋“œ๋Š” ํ•ด๋‹น ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ ์™ธ๋ถ€์—์„œ ์ˆ˜ํ–‰ํ•œ๋‹ค.(e.g., CGCContext)

  • ์ฃผ์˜ ์‚ฌํ•ญ
    • final๋กœ ์„ ์–ธ
    • base class ์—†์ด Protocol ์‚ฌ์šฉ

Don't fight the system

  • ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ๋ฐ”๊พธ๋ ค๊ณ  ํ•˜์ง€๋งˆ๋ผ

On the other hand, be circumspect

  • software ์•ˆ์—์„œ ๊ตฌ์„ฑ์š”์†Œ๊ฐ€ ๋„ˆ๋ฌด ์ปค์ง€๊ฒŒ ํ•˜๋ฉด์•ˆ๋œ๋‹ค.
  • class๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ์ „์— class๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๋ฐฉ๋ฒ•์„ ๊ณ ๋ คํ•ด ๋ด๋ผ

์ฐธ๊ณ