Skip to content

Commit a4bfbb9

Browse files
committed
Created abstraction of Flyweight
1 parent 9aa468f commit a4bfbb9

2 files changed

Lines changed: 25 additions & 22 deletions

File tree

src/main/kotlin/oop/Flyweight/Soldier.kt

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,27 @@ data class Point(val x: Int, val y: Int)
44

55
data class Soldier(val name: String)
66

7-
class SoldierFactory(private val soldiers: MutableList<Soldier> = mutableListOf(),
8-
private val points: MutableList<Point> = mutableListOf()) {
7+
class Flyweight<A>(val values: MutableSet<A> = mutableSetOf()) {
98

10-
fun getSoldier(name: String): Soldier {
11-
if (!soldiers.map { it.name }.contains(name)) {
12-
soldiers.add(Soldier(name))
13-
}
9+
fun getFactory(value: A): A {
10+
values.add(value)
11+
return values.filter { it == value }.first()
12+
}
13+
14+
}
1415

15-
return soldiers.filter { it.name == name }.first()
16+
class SoldierFactory(val soldiers: Flyweight<Soldier> = Flyweight<Soldier>(),
17+
val points: Flyweight<Point> = Flyweight<Point>()) {
18+
19+
constructor(soldiers: List<Soldier> = emptyList(), points: List<Point> = emptyList())
20+
: this(Flyweight(soldiers.toMutableSet()), Flyweight(points.toMutableSet()))
21+
22+
fun getSoldier(name: String): Soldier {
23+
return soldiers.getFactory(Soldier(name))
1624
}
1725

1826
fun getPoint(x: Int, y: Int): Point {
19-
var point = Point(x, y)
20-
if (!points.contains(point)) {
21-
points.add(point)
22-
}
23-
24-
return points.filter { it == point }.first()
27+
return points.getFactory(Point(x, y))
2528
}
2629

2730
}

src/test/kotlin/oop/Flyweigth/FlyweightPatternShould.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,31 @@ class FlyweightPatternShould {
1212

1313
@Test
1414
fun `Have one instance when only one type of soldier is created`() {
15-
val soldiers = mutableListOf<Soldier>()
16-
val soldiersAttack = SoldierClient(SoldierFactory(soldiers))
15+
val flyweight = Flyweight<Soldier>()
16+
val soldiersAttack = SoldierClient(SoldierFactory(flyweight))
1717

1818
soldiersAttack.attack("Pedro", 1, 2)
1919
soldiersAttack.attack("Pedro", 2, 2)
2020
soldiersAttack.attack("Pedro", 2, 2)
2121

22-
assertThat(soldiers.filter { it.name == "Pedro" }.size, `is`(1))
22+
assertThat(flyweight.values.filter { it.name == "Pedro" }.size, `is`(1))
2323
}
2424

2525
@Test
2626
fun `Have two instances when both types are created, no matter the number of soldiers`() {
27-
val soldiers = mutableListOf<Soldier>()
28-
val soldierFactory = SoldierFactory(soldiers)
27+
val flyweight = Flyweight<Soldier>()
28+
val soldierFactory = SoldierFactory(flyweight)
2929
(1..1000).forEach {
3030
soldierFactory.getSoldier("Pedro")
3131
soldierFactory.getSoldier("Ryan")
3232
}
3333

34-
assertThat(soldiers.size, `is`(2))
34+
assertThat(flyweight.values.size, `is`(2))
3535
}
3636

3737
@Test
3838
fun `Have the same main object in two instances of the same type`() {
39-
val soldierFactory = SoldierFactory()
39+
val soldierFactory = SoldierFactory(Flyweight<Soldier>())
4040
val soldierOne = soldierFactory.getSoldier("Pedro")
4141
val soldierTwo = soldierFactory.getSoldier("Pedro")
4242

@@ -45,13 +45,13 @@ class FlyweightPatternShould {
4545

4646
@Test
4747
fun `Have one instance of points when only one instance of point is created`() {
48-
val points = mutableListOf<Point>()
48+
val points :Flyweight<Point> = Flyweight<Point>()
4949
val soldiersAttack = SoldierClient(SoldierFactory(points = points))
5050

5151
soldiersAttack.attack("Pedro", 3, 3)
5252
soldiersAttack.attack("Forest", 3, 3)
5353
soldiersAttack.attack("Forest", 3, 3)
5454

55-
assertThat(points.size, `is`(1))
55+
assertThat(points.values.size, `is`(1))
5656
}
5757
}

0 commit comments

Comments
 (0)