File tree Expand file tree Collapse file tree
main/kotlin/oop/Flyweight
test/kotlin/oop/Flyweigth Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ class Admiral : Soldier {
77 val TYPE = 1
88 }
99 init {
10- Flyweitght .objectInstances++
10+ Flyweight .objectInstances++
1111 }
1212
1313 val attack = 6
Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ class Captain : Soldier {
77 val TYPE = 2
88 }
99 init {
10- Flyweitght .objectInstances++
10+ Flyweight .objectInstances++
1111 }
1212
1313 val attack = 10
Original file line number Diff line number Diff line change 11package oop.Flyweight
22
33import java.awt.Point
4- class Flyweitght {
4+ class Flyweight {
55 companion object {
66 var objectInstances = 0
77 }
@@ -21,6 +21,6 @@ fun main(args: Array<String>) {
2121 soldiers[3 ].attack(Point (11 ,7 ))
2222 soldiers[4 ].attack(Point (21 ,3 ))
2323
24- System .out .println (Flyweitght .objectInstances)
24+ System .out .println (Flyweight .objectInstances)
2525
2626}
Original file line number Diff line number Diff line change 11package oop.Flyweight
22
3+ import org.jetbrains.annotations.TestOnly
34import java.awt.Point
45
56class SoldierClient (private val type : Int ) {
@@ -10,4 +11,7 @@ class SoldierClient(private val type: Int) {
1011 soldier.attack(currentPosition,attackPoint)
1112 }
1213
14+ @TestOnly
15+ fun getSoldier () = soldier
16+
1317}
Original file line number Diff line number Diff line change 11package oop.Flyweight
22
3+ import org.jetbrains.annotations.TestOnly
4+
35class SoldierFactory {
46 companion object {
57 var admiral: Admiral ? = null
@@ -22,6 +24,12 @@ class SoldierFactory {
2224 }
2325 throw IllegalArgumentException ()
2426 }
27+
28+ @TestOnly
29+ fun clearInstances (){
30+ admiral = null
31+ captain = null
32+ }
2533 }
2634
2735
Original file line number Diff line number Diff line change 1+ package oop.Flyweigth
2+
3+ import oop.Flyweight.*
4+ import org.hamcrest.CoreMatchers.`is`
5+ import org.hamcrest.MatcherAssert.assertThat
6+ import org.junit.Before
7+ import org.junit.Test
8+ import java.util.*
9+
10+ class FlyweightPatternShould {
11+
12+ @Before
13+ fun initTests (){
14+ Flyweight .objectInstances = 0
15+ SoldierFactory .clearInstances()
16+ }
17+
18+ @Test
19+ fun `Have one instance when only one type of soldier is created` () {
20+ SoldierClient (Admiral .TYPE )
21+ SoldierClient (Admiral .TYPE )
22+ SoldierClient (Admiral .TYPE )
23+
24+ assertThat(Flyweight .objectInstances, `is `(1 ))
25+ }
26+ @Test
27+ fun `Have two instances when both types are created, no matter the number of soldiers` (){
28+ (1 .. Random ().nextInt(30 )).forEach {
29+ SoldierClient (Admiral .TYPE )
30+ SoldierClient (Captain .TYPE )
31+ }
32+
33+ assertThat(Flyweight .objectInstances, `is `(2 ))
34+ }
35+
36+ @Test
37+ fun `Have the same main object in two instances of the same type` (){
38+ val soldierOne: SoldierClient = SoldierClient (Admiral .TYPE )
39+ val soldierTwo: SoldierClient = SoldierClient (Admiral .TYPE )
40+
41+ assertThat(soldierOne.getSoldier(), `is `(soldierTwo.getSoldier()))
42+ }
43+ }
You can’t perform that action at this time.
0 commit comments