상세 컨텐츠

λ³Έλ¬Έ 제λͺ©

[ Swift ] ν”„λ‘œν† μ½œ(protocol)

🍎 iOS/Swift

by AHN.Jihyeon 2024. 8. 25. 08:01

λ³Έλ¬Έ

 


 

 

 

 

πŸ”Ž  1 . ν”„λ‘œν† μ½œ


ν”„λ‘œν† μ½œμ€ νŠΉμ • κΈ°λŠ₯μ΄λ‚˜ νŠΉμ„±μ„ κ΅¬ν˜„ν•  λ•Œ ν•„μš”ν•œ λ©”μ„œλ“œλ‚˜ ν”„λ‘œνΌν‹°μ˜ 청사진을 μ œκ³΅ν•œλ‹€.

ν”„λ‘œν† μ½œμ„ μ„ μ–Έν•  λ•ŒλŠ” ν”„λ‘œνΌν‹°λ‚˜ λ©”μ„œλ“œλ₯Ό ꡬ체적인 λ‚΄μš©μ΄ μ—†κ³  ν˜•νƒœλ§Œ μ œκ³΅ν•œλ‹€.

 

ꡬ체적인 λ‚΄μš©μ€ 이 ν”„λ‘œν† μ½œμ„ μ±„νƒν•˜λŠ” 클래슀, ꡬ쑰체, μ—΄κ±°ν˜•, extension κ°μ²΄μ—μ„œ λ‹΄λ‹Ήν•œλ‹€. 

ν”„λ‘œν† μ½œμ— μ •μ˜λœ λ©”μ„œλ“œμ™€ ν”„λ‘œνΌν‹°λ₯Ό λ°˜λ“œμ‹œ κ΅¬ν˜„ ν•œλ‹€.

 

 

protocol Drawable {
	var name: String { get }  // 읽기 μ „μš© ν”„λ‘œνΌν‹° 
    
    func draw ()
}


//ν”„λ‘œν† μ½œ 채택
class Circle: Drawable {

	var name: String {   //계산 ν”„λ‘œνΌν‹° 
    	return "Circle"
    }
    
    func draw() {
        print("동그라미 그리기")  //ν”„λ‘œν† μ½œ λ©”μ„œλ“œ κ΅¬ν˜„
    }
}

struct Square: Drawable {

	var name: String = "Square"  // μ €μž₯ ν”„λ‘œνΌν‹°
    
    func draw() {
        print("λ„€λͺ¨ 그리기")  //ν”„λ‘œν† μ½œ λ©”μ„œλ“œ κ΅¬ν˜„
    }
}

//ν”„λ‘œν† μ½œμ˜ μž₯점: λ‹€ν˜•μ„±(Polymorphism) 제곡
let shapes: [Drawable] = [Circle(), Square()]  //Drawable ν”„λ‘œν† μ½œμ„ μ€€μˆ˜ν•˜λŠ” κ°μ²΄λ“€λ‘œ ꡬ성

for shape in shapes {
	print("\(shape.name) : ", terminator: "")
    shape.draw()
}

//좜λ ₯
//Circle : 동그라미 그리기
//Square : λ„€λͺ¨ 그리기

 

ν”„λ‘œνΌν‹°κ°€ μ—°μ‚° ν”„λ‘œνΌν‹° 일 λ•Œ: { get} , {get set} κ°€λŠ₯

ν”„λ‘œνΌν‹°κ°€ μ €μž₯ ν”„λ‘œνΌν‹° 일 λ•Œ: λ°˜λ“œμ‹œ {get set} λͺ¨λ‘ μΆ”κ°€   

 

 

 

 

 

 

πŸ”Ž  2. ν”„λ‘œν† μ½œμ—μ„œ mutatingκ³Ό static


ꡬ쑰체 μ•ˆμ— μžˆλŠ” λ©”μ„œλ“œκ°€ ν”„λ‘œνΌν‹°λ₯Ό λ³€κ²½ν•  경우 λ©”μ„œλ“œ μ•žμ— mutating ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•΄μ•Όν•œλ‹€. 

ν”„λ‘œν† μ½œμ—μ„œλ„ λ©”μ„œλ“œμ— mutating ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•œλ‹€λ©΄ 이λ₯Ό κ΅¬ν˜„ν•˜λŠ” ꡬ쑰체와 μ—΄κ±°ν˜•μ—μ„œλ„ mutating ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•΄μ•Ό ν•œλ‹€. 

 

static ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•΄ νƒ€μž… λ©”μ„œλ“œμ™€ νƒ€μž… ν”„λ‘œνΌν‹°λ„ ν”„λ‘œν† μ½œμ—μ„œ μ •μ˜ν•  수 μžˆλ‹€. 

단, ν΄λž˜μŠ€μ—μ„œ μ‚¬μš© κ°€λŠ₯ν•œ class ν‚€μ›Œλ“œλŠ” μ‚¬μš© λΆˆκ°€ν•˜λ‹€. 

-> ν”„λ‘œν† μ½œμ€ 클래슀, ꡬ쑰체, μ—΄κ±°ν˜• λͺ¨λ‘μ—μ„œ μ‚¬μš©ν•  수 μžˆμ–΄μ•Ό ν•˜κΈ° λ•Œλ¬Έ.

 

 

 

 

 

πŸ”Ž  3. ν”„λ‘œν† μ½œκ³Ό μ΄ˆκΈ°ν™” 


ν΄λž˜μŠ€μ—μ„œ μ΄ˆκΈ°ν™” λ©”μ„œλ“œλ₯Ό κ΅¬ν˜„ν•  λ•ŒλŠ” λ°˜λ“œμ‹œ required ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•œλ‹€. 

 

 

import Foundation

protocol Initializable {
    init(name: String)
}

//ꡬ쑰체
struct User: Initializable {
    var name: String
    
    // ν”„λ‘œν† μ½œμ˜ μš”κ΅¬ 사항을 μΆ©μ‘±ν•˜λŠ” μ΄ˆκΈ°ν™” λ©”μ„œλ“œ
    init(name: String) {
        self.name = name
    }
}


//클래슀
class Person: Initializable {
    var name: String
    
    // ν”„λ‘œν† μ½œμ˜ μš”κ΅¬ 사항을 μΆ©μ‘±ν•˜λŠ” μ΄ˆκΈ°ν™” λ©”μ„œλ“œ
    required init(name: String) {
        self.name = name
    }
}


//μ—΄κ±°ν˜•
protocol Initializable {
    init(rawValue: String)
}

enum Status: String, Initializable {
    case active = "Active"
    case inactive = "Inactive"
    
    // ν”„λ‘œν† μ½œμ˜ μš”κ΅¬ 사항을 μΆ©μ‘±ν•˜λŠ” μ΄ˆκΈ°ν™” λ©”μ„œλ“œ
    init(rawValue: String) {
        self = Status(rawValue: rawValue) ?? .inactive
    }
}

 

 

 

 

 

πŸ”Ž  4. νƒ€μž…μœΌλ‘œμ„œ ν”„λ‘œν† μ½œ


ν”„λ‘œν† μ½œμ„ νƒ€μž…μœΌλ‘œ μ‚¬μš©ν•  경우 λ‹€ν˜•μ„±κ³Ό μ˜μ‘΄μ„± μ—­μ „ 원칙을 μ μš©ν•  수 μžˆλ‹€.

즉, λ‹€μ–‘ν•œ νƒ€μž…μ˜ μΈμŠ€ν„΄μŠ€λ“€μ„ κ³΅ν†΅λœ μΈν„°νŽ˜μ΄μŠ€λ₯Ό 톡해 μ²˜λ¦¬ν•  수 μžˆλ‹€.

 

ν”„λ‘œν† μ½œ νƒ€μž…μœΌλ‘œ μ •μ˜λœ λ³€μˆ˜λ‚˜ μƒμˆ˜μ— ν• λ‹Ήλœ κ°μ²΄λŠ” ν”„λ‘œν† μ½œμ—μ„œλ§Œ μ„ μ–Έλœ ν”„λ‘œνΌν‹°μ™€ λ©”μ„œλ“œμ—λ§Œ μ ‘κ·Ό κ°€λŠ₯ν•˜λ‹€. 

κ΅¬ν˜„μ—μ„œ μΆ”κ°€ν•œ ν”„λ‘œνΌν‹°λ‚˜ λ©”μ„œλ“œλ“€μ€ 컴파일러둜 λΆ€ν„° μ€λ‹‰λœλ‹€. 

μΈμŠ€ν„΄μŠ€ 자체의 λ©”μ„œλ“œλ‚˜ ν”„λ‘œνΌν‹°λ₯Ό μ‚¬μš©ν•˜κ³  μ‹Άλ‹€λ©΄ asλ₯Ό 톡해 λ‹€μš΄μΊμŠ€νŒ… ν›„ μ‚¬μš© ν•œλ‹€. 

 

 

 

1. λ‹€ν˜•μ„±

//λ‹€ν˜•μ„±
protocol Drawable {
    func draw()
}

class Circle: Drawable {
    func draw() {
        print("Drawing a circle")
    }
}

class Square: Drawable {
    func draw() {
        print("Drawing a square")
    }
}

//ν™œμš©1.
let shapes: [Drawable] = [Circle(), Square()]  //Drawable ν”„λ‘œν† μ½œμ„ μ€€μˆ˜ν•˜λŠ” κ°μ²΄λ“€λ‘œ ꡬ성

for shape in shapes {
    shape.draw()
}

//ν™œμš©2.
let circle = Circle()
let square = Square()

render(shape: circle)  
render(shape: square)

 

 

2. μ˜μ‘΄μ„± μ—­μ „ 원칙

//μ˜μ‘΄μ„± μ—­μ „ 원칙
class DocumentManager {
    var drawable: Drawable

    init(drawable: Drawable) {
        self.drawable = drawable
    }

    func renderDocument() {
        drawable.draw()
    }
}

let circle = Circle()
let documentManager = DocumentManager(drawable: circle)
documentManager.renderDocument()  // Output: Drawing a circle

 

 

 

 

 

πŸ”— Reference 


 

κ΄€λ ¨κΈ€ 더보기