-
Notifications
You must be signed in to change notification settings - Fork 14
/
Visitor.kt
81 lines (72 loc) · 2.5 KB
/
Visitor.kt
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package org.vld.sdp.behavioral
/**
* Element interface
*/
interface Phone {
// visitor operation for every Visitor type
// dynamic dispatch on the abstract Visitor interface type
fun switchOnWiFi(wiFi: WiFi): String // WiFi visitor
fun takePhoto(camera: Camera): String // Camera visitor
}
/**
* Element interface implementation
*/
class XiaomiPhone : Phone {
// dynamic Visitor dispatch trivial implementation
override fun switchOnWiFi(wiFi: WiFi) = wiFi.switchOn(this)
override fun takePhoto(camera: Camera) = camera.takePhoto(this)
}
/**
* Element interface implementation
*/
class NokiaPhone : Phone {
// dynamic Visitor dispatch trivial implementation
override fun switchOnWiFi(wiFi: WiFi): String = wiFi.switchOn(this)
override fun takePhoto(camera: Camera): String = camera.takePhoto(this)
}
/**
* WiFi Visitor interface
*/
interface WiFi {
// visit method overloaded for each Element type implements a new operation over all Elements
// static dispatch on the concrete Element type
fun switchOn(phone: XiaomiPhone): String
fun switchOn(phone: NokiaPhone): String
}
/**
* WiFi Visitor interface implementation
*/
class IntelWiFi : WiFi {
override fun switchOn(phone: XiaomiPhone): String = "Switch on Intel WiFi on Xiaomi Phone"
override fun switchOn(phone: NokiaPhone): String = "Switch on Intel WiFi on Nokia Phone"
}
/**
* WiFi Visitor interface implementation
*/
class BroadcomWiFi : WiFi {
override fun switchOn(phone: XiaomiPhone): String = "Switch on Broadcom WiFi on Xiaomi Phone"
override fun switchOn(phone: NokiaPhone): String = "Switch on Broadcom WiFi on Nokia Phone"
}
/**
* Camera Visitor interface
*/
interface Camera {
// visit method overloaded for each Element type implements a new operation over all Elements
// static dispatch on the concrete Element type
fun takePhoto(phone: XiaomiPhone): String
fun takePhoto(phone: NokiaPhone): String
}
/**
* Camera Visitor interface implementation
*/
class SonyCamera : Camera {
override fun takePhoto(phone: XiaomiPhone): String = "Take photo with Sony Camera on Xiaomi Phone"
override fun takePhoto(phone: NokiaPhone): String = "Take photo with Sony Camera on Nokia Phone"
}
/**
* Camera Visitor interface implementation
*/
class SamsungCamera : Camera {
override fun takePhoto(phone: XiaomiPhone): String = "Take photo with Samsung Camera on Xiaomi Phone"
override fun takePhoto(phone: NokiaPhone): String = "Take photo with Samsung Camera on Nokia Phone"
}