-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdapter.kt
36 lines (29 loc) · 845 Bytes
/
Adapter.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
package adapter
interface Duck {
fun quack()
fun flyLongDistance()
}
interface Turkey {
fun gobble()
fun flyShortDistance()
}
class MallardDuck : Duck {
override fun quack() = println("\'Quack!\'")
override fun flyLongDistance() = println("Duck flies long distance.")
}
class WildTurkey : Turkey {
override fun gobble() = println("\'Gobble-gobble\'")
override fun flyShortDistance() = println("Turkey flies quite a bit.")
}
class TurkeyAdapter(private val turkey: Turkey) : Duck {
override fun quack() = turkey.gobble()
override fun flyLongDistance() {
for (i in 0 until 5) turkey.flyShortDistance()
}
}
fun huntDuck(duck: Duck) {
// hunter will only shoot if the "duck" quacks and flies far
duck.quack()
duck.flyLongDistance()
println("Seems like it duck. \'BANG!\'")
}