ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • (Swift) POP (Protocol Oriented Programming)
    iOS💖 2024. 9. 4. 16:02

    protocol , extension , protocol extension 을 통해 POP (Protocol Oriented Programming)이 가능함

     

     

    1. protocol : 어떤 역할에 대한 정의 제공

    • 실제 구현 제공 X
    protocol Payable {
        func calculateWages() -> Int
    }
    
    protocol Trainable {
        func train()
    }
    
    protocol HasVacation {
        func takeVacation(days: Int)
    }
    
    protocol Employee: Payable, Trainable, HasVacation {}
    
    struct DeveloperEmployee: Employee {
        var name: String
        
        func calculateWages() -> Int {
            return 10_000_000
        }
    
        func train() {
            print("study hard")
        }
    
        func takeVacation(days: Int) {
            print("take \(days) days off")
        }
    }
    
    let choi = DeveloperEmployee(name: "Choi")
    print(choi.calculateWages())
    choi.train()
    choi.takeVacation(days: 3)

     

     

     

     2. extension : 어떤 타입에 대한 구현 제공

    • 한 가지 타입에만 적용됨
    extension Int {
        func squared() -> Int {
            return self * self
        }
    }
    
    let number = 8
    print(number.squared())
    
    extension Int {
        var isEven: Bool {
            return self % 2 == 0
        }
    }
    print(number.isEven)

     

     

     

     

      3. protocol extension

    • 역할에 대한 실제 구현을 제공하지 않는 protocol 에,
      extension (확장)을 통해, 역할에 대한 실제 구현 (= 메소드 구현) 가능함
    protocol Payable {
        func calculateWages() -> Int
    }
    
    protocol Trainable {
        func train()
    }
    
    protocol HasVacation {
        func takeVacation(days: Int)
    }
    
    // ⭐️
    extension Payable { 
        func calculateWages() -> Int {
            return 10_000_000
        }
    }
    
    // ⭐️
    extension Trainable { 
        func train() {
            print("(extension) study hard!")
        }
    }
    
    // ⭐️
    extension HasVacation {  
        func takeVacation(days: Int) {
            print("(extension) take \(days) days off")
        }
    }
    
    protocol Employee: Payable, Trainable, HasVacation {}
    
    struct DeveloperEmployee: Employee {
        var name: String
    }
    
    let choi = DeveloperEmployee(name: "Choi")
    print(choi.calculateWages())
    choi.train()
    choi.takeVacation(days: 3)
    
    struct DesignerEmployee: Employee {
        var name: String
    }
    
    let jade = DesignerEmployee(name: "Jade")
    print(jade.calculateWages())
    jade.train()
    jade.takeVacation(days: 5)

     

     

     

    출처 : 왕초보를 위한 한 번에 끝내는 iOS 앱 개발 바이블 초격차 패키지 Online.

    'iOS💖' 카테고리의 다른 글

    (Swift) CollectionView contentInset (padding 주는 방법)  (4) 2024.09.10
    (iOS) Life Cycle (AppDelegate / SceneDelegate)  (4) 2024.09.06
    (Swift) Structures VS. Classes  (0) 2024.09.03
    (Swift) deinitializer  (3) 2024.09.03
    (Swift) Access Control  (2) 2024.09.03

    댓글

Designed by black7375.