-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProtocolsAndDelegates.swift
66 lines (49 loc) · 1.28 KB
/
ProtocolsAndDelegates.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
protocol advanceLifeSupport {
func performCPR()
}
class EmergencyCallHandler {
var delegate: advanceLifeSupport?
func assessSituation() {
print("Can you tell me what happened?")
}
func medicalEmergency() {
delegate?.performCPR()
}
}
struct Paramedic: advanceLifeSupport {
init(handler: EmergencyCallHandler) {
handler.delegate = self
}
func performCPR() {
print("The paramedic does chest compression, 30 per second.")
}
}
class Doctor: advanceLifeSupport {
init(handler: EmergencyCallHandler) {
handler.delegate = self
}
func performCPR() {
print("Doctor does chest compression, 30 per second.")
}
func useStethescope() {
print("Listening for heart sounds...")
}
}
class Surgeon: Doctor {
override func performCPR() {
super.performCPR()
print("Sing staying alive by BeeGees")
}
func useElectricDrill() {
print("Whirrrr....")
}
}
let emilio = EmergencyCallHandler()
let mark = Paramedic(handler: emilio)
let nan = Doctor(handler: emilio)
let angela = Surgeon(handler: emilio)
emilio.assessSituation()
emilio.medicalEmergency()
mark.performCPR()
nan.useStethescope()
angela.useElectricDrill()