Skip to content

Commit 82781a9

Browse files
authored
mediator (#743)
* commit * commit * update * update * update * update
1 parent 5c0b03f commit 82781a9

File tree

1 file changed

+46
-0
lines changed
  • kotlin-patterns/src/main/kotlin/com/baeldung/mediator

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.mediator
2+
3+
interface AirTrafficController {
4+
fun registerAirplane(airplane: Airplane)
5+
fun deregisterAirplane(airplane: Airplane)
6+
fun requestTakeOff(airplane: Airplane)
7+
fun requestLanding(airplane: Airplane)
8+
}
9+
class Airplane(private val registrationNumber: String, private val controller: AirTrafficController) {
10+
fun takeOff() {
11+
println("$registrationNumber is requesting takeoff.")
12+
controller.requestTakeOff(this)
13+
}
14+
fun land() {
15+
println("$registrationNumber is requesting landing.")
16+
controller.requestLanding(this)
17+
}
18+
fun notifyTakeOff() {
19+
println("$registrationNumber has taken off.")
20+
controller.deregisterAirplane(this)
21+
}
22+
fun notifyLanding() {
23+
println("$registrationNumber has landed.")
24+
controller.registerAirplane(this)
25+
}
26+
}
27+
28+
class AirTrafficControlTower : AirTrafficController {
29+
private val registeredAirplanes: MutableSet<Airplane> = mutableSetOf()
30+
override fun registerAirplane(airplane: Airplane) {
31+
registeredAirplanes.add(airplane)
32+
}
33+
override fun deregisterAirplane(airplane: Airplane) {
34+
registeredAirplanes.remove(airplane)
35+
}
36+
override fun requestTakeOff(airplane: Airplane) {
37+
if (registeredAirplanes.contains(airplane)) {
38+
airplane.notifyTakeOff()
39+
}
40+
}
41+
override fun requestLanding(airplane: Airplane) {
42+
if (!registeredAirplanes.contains(airplane)) {
43+
airplane.notifyLanding()
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)