protocol 을 유용하게 쓰는방법으로 delegation pattern 존재한다
delegation 대행자로써 protocol 상속받은 class,struct의 내장함수를 실행해주는것으로 유사한기능의 class,struct에 적용하면 모듈을 효율적이게 작성가능
protocol AppleDevice {
var serialNumber:String {
get
}
func findMyDevice()
}
struct iPhone: AppleDevice {
init(master:AppleCompany) {
master.delegate = self
}
var serialNumber: String {
var SerialNumber = "iPhone"
for i in 0...15 {
SerialNumber += String(Int.random(in: 0...9))
}
return SerialNumber
}
func findMyDevice() {
print("Find My Iphone...")
}
}
struct Ipad: AppleDevice {
init(master:AppleCompany) {
master.delegate = self
}
var serialNumber: String {
var SerialNumber = "iPad"
for i in 0...15 {
SerialNumber += String(Int.random(in: 0...9))
}
return SerialNumber
}
func findMyDevice() {
print("Find My Ipad...")
}
}
class AppleCompany {
var delegate:AppleDevice?
func RecallDevice() {
print("Recall device")
}
func forcedFindMyDevice() {
delegate?.findMyDevice()
}
}
let steave = AppleCompany()
let iPhone13 = iPhone(master:steave)
steave.forcedFindMyDevice()
steave.RecallDevice()
let IpadAir = Ipad(master: steave)
steave.forcedFindMyDevice()
steave 대행자를 통해서 iPhone,Ipad의 findMyDevice 실행시킬수있다....