-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSoldier.kt
More file actions
47 lines (33 loc) · 1.26 KB
/
Soldier.kt
File metadata and controls
47 lines (33 loc) · 1.26 KB
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
package oop.Flyweight
data class Point(val x: Int, val y: Int)
data class Soldier(val name: String)
class Flyweight<A>(val values: MutableSet<A> = mutableSetOf()) {
fun getFactory(value: A): A {
values.add(value)
return values.filter { it == value }.first()
}
}
class SoldierFactory(val soldiers: Flyweight<Soldier> = Flyweight<Soldier>(),
val points: Flyweight<Point> = Flyweight<Point>()) {
constructor(soldiers: List<Soldier> = emptyList(), points: List<Point> = emptyList())
: this(Flyweight(soldiers.toMutableSet()), Flyweight(points.toMutableSet()))
fun getSoldier(name: String): Soldier {
return soldiers.getFactory(Soldier(name))
}
fun getPoint(x: Int, y: Int): Point {
return points.getFactory(Point(x, y))
}
}
data class Attack(val soldier: Soldier, val point: Point) {
override fun toString(): String {
return "$soldier attack ${point.x},${point.y} point"
}
}
class SoldierClient(private val soldierFactory: SoldierFactory,
val attacks: MutableList<Attack> = mutableListOf<Attack>()) {
fun attack(soldierName: String, x: Int, y: Int) {
val soldier = soldierFactory.getSoldier(soldierName)
val point = soldierFactory.getPoint(x, y)
attacks.add(Attack(soldier, point))
}
}