forked from huangyu/KotlinDesignPatterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMediator.kt
More file actions
36 lines (28 loc) · 744 Bytes
/
Mediator.kt
File metadata and controls
36 lines (28 loc) · 744 Bytes
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 com.huangyu.mediator
interface IMediator {
fun register(colleagur: AColleague)
fun operation(i: Int)
}
class Mediator : IMediator {
var colleagues: MutableList<AColleague> = mutableListOf()
override fun register(colleagur: AColleague) {
colleagues.add(colleagur)
}
override fun operation(i: Int) {
colleagues.get(i).operation()
}
}
abstract class AColleague(val mediator: IMediator) {
abstract fun selfMethod()
abstract fun communicate(i: Int)
abstract fun operation()
}
class ConcreteColleague(val name: String, mediator: IMediator) : AColleague(mediator) {
override fun selfMethod() {}
override fun communicate(i: Int) {
mediator.operation(i)
}
override fun operation() {
println(name + "operation")
}
}