From 1d9945d09e5e053224ff0309b98ed0373c14b530 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20David=20Rodr=C3=ADguez=20S=C3=A1nchez?= Date: Thu, 30 Mar 2017 10:57:52 +0200 Subject: [PATCH 1/5] Created flyweight pattern --- src/main/kotlin/oop/Flyweight/Admiral.kt | 19 +++++++++++++ src/main/kotlin/oop/Flyweight/Captain.kt | 19 +++++++++++++ src/main/kotlin/oop/Flyweight/Flyweitght.kt | 26 +++++++++++++++++ src/main/kotlin/oop/Flyweight/Soldier.kt | 7 +++++ .../kotlin/oop/Flyweight/SoldierClient.kt | 13 +++++++++ .../kotlin/oop/Flyweight/SoldierFactory.kt | 28 +++++++++++++++++++ 6 files changed, 112 insertions(+) create mode 100644 src/main/kotlin/oop/Flyweight/Admiral.kt create mode 100644 src/main/kotlin/oop/Flyweight/Captain.kt create mode 100644 src/main/kotlin/oop/Flyweight/Flyweitght.kt create mode 100644 src/main/kotlin/oop/Flyweight/Soldier.kt create mode 100644 src/main/kotlin/oop/Flyweight/SoldierClient.kt create mode 100644 src/main/kotlin/oop/Flyweight/SoldierFactory.kt diff --git a/src/main/kotlin/oop/Flyweight/Admiral.kt b/src/main/kotlin/oop/Flyweight/Admiral.kt new file mode 100644 index 0000000..0fe35e4 --- /dev/null +++ b/src/main/kotlin/oop/Flyweight/Admiral.kt @@ -0,0 +1,19 @@ +package oop.Flyweight + +import java.awt.Point + +class Admiral : Soldier { + companion object { + val TYPE = 1 + } + init { + Flyweitght.objectInstances++ + } + + val attack = 6 + val salary = 23000 + override fun attack(currentPosition: Point, attackPosition: Point) { + currentPosition.move(attackPosition.x, attackPosition.y) + } + +} diff --git a/src/main/kotlin/oop/Flyweight/Captain.kt b/src/main/kotlin/oop/Flyweight/Captain.kt new file mode 100644 index 0000000..28516f5 --- /dev/null +++ b/src/main/kotlin/oop/Flyweight/Captain.kt @@ -0,0 +1,19 @@ +package oop.Flyweight + +import java.awt.Point + +class Captain : Soldier { + companion object { + val TYPE = 2 + } + init { + Flyweitght.objectInstances++ + } + + val attack = 10 + val salary = 10000 + override fun attack(currentPosition: Point, attackPosition: Point) { + currentPosition.move(attackPosition.x, attackPosition.y) + } + +} diff --git a/src/main/kotlin/oop/Flyweight/Flyweitght.kt b/src/main/kotlin/oop/Flyweight/Flyweitght.kt new file mode 100644 index 0000000..526d032 --- /dev/null +++ b/src/main/kotlin/oop/Flyweight/Flyweitght.kt @@ -0,0 +1,26 @@ +package oop.Flyweight + +import java.awt.Point +class Flyweitght{ + companion object{ + var objectInstances = 0 + } +} +fun main(args: Array) { + val soldiers = listOf( + SoldierClient(Admiral.TYPE), + SoldierClient(Admiral.TYPE), + SoldierClient(Captain.TYPE), + SoldierClient(Captain.TYPE), + SoldierClient(Admiral.TYPE), + SoldierClient(Admiral.TYPE) + ) + soldiers[0].attack(Point(1,2)) + soldiers[1].attack(Point(10,12)) + soldiers[2].attack(Point(9,5)) + soldiers[3].attack(Point(11,7)) + soldiers[4].attack(Point(21,3)) + + System.out.println(Flyweitght.objectInstances) + +} diff --git a/src/main/kotlin/oop/Flyweight/Soldier.kt b/src/main/kotlin/oop/Flyweight/Soldier.kt new file mode 100644 index 0000000..35c727c --- /dev/null +++ b/src/main/kotlin/oop/Flyweight/Soldier.kt @@ -0,0 +1,7 @@ +package oop.Flyweight + +import java.awt.Point + +interface Soldier { + fun attack(currentPosition: Point, attackPosition: Point) +} diff --git a/src/main/kotlin/oop/Flyweight/SoldierClient.kt b/src/main/kotlin/oop/Flyweight/SoldierClient.kt new file mode 100644 index 0000000..01c9ebe --- /dev/null +++ b/src/main/kotlin/oop/Flyweight/SoldierClient.kt @@ -0,0 +1,13 @@ +package oop.Flyweight + +import java.awt.Point + +class SoldierClient(private val type: Int) { + private val soldier: Soldier = SoldierFactory.getSoldier(type) + private val currentPosition: Point = Point() + + fun attack(attackPoint: Point){ + soldier.attack(currentPosition,attackPoint) + } + +} diff --git a/src/main/kotlin/oop/Flyweight/SoldierFactory.kt b/src/main/kotlin/oop/Flyweight/SoldierFactory.kt new file mode 100644 index 0000000..773289d --- /dev/null +++ b/src/main/kotlin/oop/Flyweight/SoldierFactory.kt @@ -0,0 +1,28 @@ +package oop.Flyweight + +class SoldierFactory { + companion object { + var admiral: Admiral? = null + var captain: Captain? = null + + fun getSoldier(type: Int): Soldier{ + when(type){ + 1 -> { + if(admiral == null){ + admiral = Admiral() + } + return admiral!! + } + 2 -> { + if(captain == null){ + captain = Captain() + } + return captain!! + } + } + throw IllegalArgumentException() + } + } + + +} From f3980356f54cf1cf324bb72ea537488deee5de1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20David=20Rodr=C3=ADguez=20S=C3=A1nchez?= Date: Thu, 30 Mar 2017 11:18:36 +0200 Subject: [PATCH 2/5] Added test and support for test --- src/main/kotlin/oop/Flyweight/Admiral.kt | 2 +- src/main/kotlin/oop/Flyweight/Captain.kt | 2 +- .../Flyweight/{Flyweitght.kt => Flyweight.kt} | 4 +- .../kotlin/oop/Flyweight/SoldierClient.kt | 4 ++ .../kotlin/oop/Flyweight/SoldierFactory.kt | 8 ++++ .../oop/Flyweigth/FlyweightPatternShould.kt | 43 +++++++++++++++++++ 6 files changed, 59 insertions(+), 4 deletions(-) rename src/main/kotlin/oop/Flyweight/{Flyweitght.kt => Flyweight.kt} (88%) create mode 100644 src/test/kotlin/oop/Flyweigth/FlyweightPatternShould.kt diff --git a/src/main/kotlin/oop/Flyweight/Admiral.kt b/src/main/kotlin/oop/Flyweight/Admiral.kt index 0fe35e4..3254833 100644 --- a/src/main/kotlin/oop/Flyweight/Admiral.kt +++ b/src/main/kotlin/oop/Flyweight/Admiral.kt @@ -7,7 +7,7 @@ class Admiral : Soldier { val TYPE = 1 } init { - Flyweitght.objectInstances++ + Flyweight.objectInstances++ } val attack = 6 diff --git a/src/main/kotlin/oop/Flyweight/Captain.kt b/src/main/kotlin/oop/Flyweight/Captain.kt index 28516f5..61c5fdc 100644 --- a/src/main/kotlin/oop/Flyweight/Captain.kt +++ b/src/main/kotlin/oop/Flyweight/Captain.kt @@ -7,7 +7,7 @@ class Captain : Soldier { val TYPE = 2 } init { - Flyweitght.objectInstances++ + Flyweight.objectInstances++ } val attack = 10 diff --git a/src/main/kotlin/oop/Flyweight/Flyweitght.kt b/src/main/kotlin/oop/Flyweight/Flyweight.kt similarity index 88% rename from src/main/kotlin/oop/Flyweight/Flyweitght.kt rename to src/main/kotlin/oop/Flyweight/Flyweight.kt index 526d032..758418c 100644 --- a/src/main/kotlin/oop/Flyweight/Flyweitght.kt +++ b/src/main/kotlin/oop/Flyweight/Flyweight.kt @@ -1,7 +1,7 @@ package oop.Flyweight import java.awt.Point -class Flyweitght{ +class Flyweight{ companion object{ var objectInstances = 0 } @@ -21,6 +21,6 @@ fun main(args: Array) { soldiers[3].attack(Point(11,7)) soldiers[4].attack(Point(21,3)) - System.out.println(Flyweitght.objectInstances) + System.out.println(Flyweight.objectInstances) } diff --git a/src/main/kotlin/oop/Flyweight/SoldierClient.kt b/src/main/kotlin/oop/Flyweight/SoldierClient.kt index 01c9ebe..4089874 100644 --- a/src/main/kotlin/oop/Flyweight/SoldierClient.kt +++ b/src/main/kotlin/oop/Flyweight/SoldierClient.kt @@ -1,5 +1,6 @@ package oop.Flyweight +import org.jetbrains.annotations.TestOnly import java.awt.Point class SoldierClient(private val type: Int) { @@ -10,4 +11,7 @@ class SoldierClient(private val type: Int) { soldier.attack(currentPosition,attackPoint) } + @TestOnly + fun getSoldier() = soldier + } diff --git a/src/main/kotlin/oop/Flyweight/SoldierFactory.kt b/src/main/kotlin/oop/Flyweight/SoldierFactory.kt index 773289d..37415d3 100644 --- a/src/main/kotlin/oop/Flyweight/SoldierFactory.kt +++ b/src/main/kotlin/oop/Flyweight/SoldierFactory.kt @@ -1,5 +1,7 @@ package oop.Flyweight +import org.jetbrains.annotations.TestOnly + class SoldierFactory { companion object { var admiral: Admiral? = null @@ -22,6 +24,12 @@ class SoldierFactory { } throw IllegalArgumentException() } + + @TestOnly + fun clearInstances(){ + admiral = null + captain = null + } } diff --git a/src/test/kotlin/oop/Flyweigth/FlyweightPatternShould.kt b/src/test/kotlin/oop/Flyweigth/FlyweightPatternShould.kt new file mode 100644 index 0000000..2415a9b --- /dev/null +++ b/src/test/kotlin/oop/Flyweigth/FlyweightPatternShould.kt @@ -0,0 +1,43 @@ +package oop.Flyweigth + +import oop.Flyweight.* +import org.hamcrest.CoreMatchers.`is` +import org.hamcrest.MatcherAssert.assertThat +import org.junit.Before +import org.junit.Test +import java.util.* + +class FlyweightPatternShould { + + @Before + fun initTests(){ + Flyweight.objectInstances = 0 + SoldierFactory.clearInstances() + } + + @Test + fun `Have one instance when only one type of soldier is created`() { + SoldierClient(Admiral.TYPE) + SoldierClient(Admiral.TYPE) + SoldierClient(Admiral.TYPE) + + assertThat(Flyweight.objectInstances, `is`(1)) + } + @Test + fun `Have two instances when both types are created, no matter the number of soldiers`(){ + (1..Random().nextInt(30)).forEach { + SoldierClient(Admiral.TYPE) + SoldierClient(Captain.TYPE) + } + + assertThat(Flyweight.objectInstances, `is`(2)) + } + + @Test + fun `Have the same main object in two instances of the same type`(){ + val soldierOne: SoldierClient = SoldierClient(Admiral.TYPE) + val soldierTwo: SoldierClient = SoldierClient(Admiral.TYPE) + + assertThat(soldierOne.getSoldier(), `is`(soldierTwo.getSoldier())) + } +} From b11a283dd558ecb3181f90d8390d4c9d18f99b1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20L=C3=B3pez=20Mar=C3=ADn?= Date: Fri, 31 Mar 2017 13:16:52 +0200 Subject: [PATCH 3/5] Refactor flyweight --- src/main/kotlin/oop/Flyweight/Admiral.kt | 19 --------- src/main/kotlin/oop/Flyweight/Captain.kt | 19 --------- src/main/kotlin/oop/Flyweight/Flyweight.kt | 32 ++++++--------- src/main/kotlin/oop/Flyweight/Soldier.kt | 32 +++++++++++++-- .../kotlin/oop/Flyweight/SoldierClient.kt | 17 -------- .../kotlin/oop/Flyweight/SoldierFactory.kt | 36 ----------------- .../oop/Flyweigth/FlyweightPatternShould.kt | 40 ++++++++++--------- 7 files changed, 63 insertions(+), 132 deletions(-) delete mode 100644 src/main/kotlin/oop/Flyweight/Admiral.kt delete mode 100644 src/main/kotlin/oop/Flyweight/Captain.kt delete mode 100644 src/main/kotlin/oop/Flyweight/SoldierClient.kt delete mode 100644 src/main/kotlin/oop/Flyweight/SoldierFactory.kt diff --git a/src/main/kotlin/oop/Flyweight/Admiral.kt b/src/main/kotlin/oop/Flyweight/Admiral.kt deleted file mode 100644 index 3254833..0000000 --- a/src/main/kotlin/oop/Flyweight/Admiral.kt +++ /dev/null @@ -1,19 +0,0 @@ -package oop.Flyweight - -import java.awt.Point - -class Admiral : Soldier { - companion object { - val TYPE = 1 - } - init { - Flyweight.objectInstances++ - } - - val attack = 6 - val salary = 23000 - override fun attack(currentPosition: Point, attackPosition: Point) { - currentPosition.move(attackPosition.x, attackPosition.y) - } - -} diff --git a/src/main/kotlin/oop/Flyweight/Captain.kt b/src/main/kotlin/oop/Flyweight/Captain.kt deleted file mode 100644 index 61c5fdc..0000000 --- a/src/main/kotlin/oop/Flyweight/Captain.kt +++ /dev/null @@ -1,19 +0,0 @@ -package oop.Flyweight - -import java.awt.Point - -class Captain : Soldier { - companion object { - val TYPE = 2 - } - init { - Flyweight.objectInstances++ - } - - val attack = 10 - val salary = 10000 - override fun attack(currentPosition: Point, attackPosition: Point) { - currentPosition.move(attackPosition.x, attackPosition.y) - } - -} diff --git a/src/main/kotlin/oop/Flyweight/Flyweight.kt b/src/main/kotlin/oop/Flyweight/Flyweight.kt index 758418c..3ae6b9d 100644 --- a/src/main/kotlin/oop/Flyweight/Flyweight.kt +++ b/src/main/kotlin/oop/Flyweight/Flyweight.kt @@ -1,26 +1,20 @@ package oop.Flyweight -import java.awt.Point -class Flyweight{ - companion object{ - var objectInstances = 0 - } -} fun main(args: Array) { - val soldiers = listOf( - SoldierClient(Admiral.TYPE), - SoldierClient(Admiral.TYPE), - SoldierClient(Captain.TYPE), - SoldierClient(Captain.TYPE), - SoldierClient(Admiral.TYPE), - SoldierClient(Admiral.TYPE) + val soldiers = mutableListOf( + Soldier("Pedro"), + Soldier("Ryan"), + Soldier("Forest") ) - soldiers[0].attack(Point(1,2)) - soldiers[1].attack(Point(10,12)) - soldiers[2].attack(Point(9,5)) - soldiers[3].attack(Point(11,7)) - soldiers[4].attack(Point(21,3)) - System.out.println(Flyweight.objectInstances) + val soldiersAttacks = SoldierClient(SoldierFactory(soldiers)) + soldiersAttacks.attack("Pedro", Point(1,2)) + soldiersAttacks.attack("Ryan", Point(10,12)) + soldiersAttacks.attack("Ryan", Point(13,2)) + soldiersAttacks.attack("Forest", Point(133,233)) + soldiersAttacks.attack("Pedro", Point(1,1)) + soldiersAttacks.attack("Pedro", Point(2,2)) + soldiersAttacks.attack("Pedro", Point(44,5)) + soldiersAttacks.attacks.forEach(::println) } diff --git a/src/main/kotlin/oop/Flyweight/Soldier.kt b/src/main/kotlin/oop/Flyweight/Soldier.kt index 35c727c..0ec70ba 100644 --- a/src/main/kotlin/oop/Flyweight/Soldier.kt +++ b/src/main/kotlin/oop/Flyweight/Soldier.kt @@ -1,7 +1,33 @@ package oop.Flyweight -import java.awt.Point +data class Point(val x: Int, val y: Int) + +data class Soldier(val name: String) + +class SoldierFactory(private val soldiers: MutableList = mutableListOf()) { + + fun getSoldier(name: String): Soldier { + if (!soldiers.map { it.name }.contains(name)) { + soldiers.add(Soldier(name)) + } + + return soldiers.filter { it.name == name }.first() + } + +} + +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 = mutableListOf()) { + + fun attack(soldierName: String, point: Point) { + val soldier = soldierFactory.getSoldier(soldierName) + attacks.add(Attack(soldier, point)) + } -interface Soldier { - fun attack(currentPosition: Point, attackPosition: Point) } diff --git a/src/main/kotlin/oop/Flyweight/SoldierClient.kt b/src/main/kotlin/oop/Flyweight/SoldierClient.kt deleted file mode 100644 index 4089874..0000000 --- a/src/main/kotlin/oop/Flyweight/SoldierClient.kt +++ /dev/null @@ -1,17 +0,0 @@ -package oop.Flyweight - -import org.jetbrains.annotations.TestOnly -import java.awt.Point - -class SoldierClient(private val type: Int) { - private val soldier: Soldier = SoldierFactory.getSoldier(type) - private val currentPosition: Point = Point() - - fun attack(attackPoint: Point){ - soldier.attack(currentPosition,attackPoint) - } - - @TestOnly - fun getSoldier() = soldier - -} diff --git a/src/main/kotlin/oop/Flyweight/SoldierFactory.kt b/src/main/kotlin/oop/Flyweight/SoldierFactory.kt deleted file mode 100644 index 37415d3..0000000 --- a/src/main/kotlin/oop/Flyweight/SoldierFactory.kt +++ /dev/null @@ -1,36 +0,0 @@ -package oop.Flyweight - -import org.jetbrains.annotations.TestOnly - -class SoldierFactory { - companion object { - var admiral: Admiral? = null - var captain: Captain? = null - - fun getSoldier(type: Int): Soldier{ - when(type){ - 1 -> { - if(admiral == null){ - admiral = Admiral() - } - return admiral!! - } - 2 -> { - if(captain == null){ - captain = Captain() - } - return captain!! - } - } - throw IllegalArgumentException() - } - - @TestOnly - fun clearInstances(){ - admiral = null - captain = null - } - } - - -} diff --git a/src/test/kotlin/oop/Flyweigth/FlyweightPatternShould.kt b/src/test/kotlin/oop/Flyweigth/FlyweightPatternShould.kt index 2415a9b..118d357 100644 --- a/src/test/kotlin/oop/Flyweigth/FlyweightPatternShould.kt +++ b/src/test/kotlin/oop/Flyweigth/FlyweightPatternShould.kt @@ -1,5 +1,6 @@ package oop.Flyweigth +import junit.framework.Assert.assertTrue import oop.Flyweight.* import org.hamcrest.CoreMatchers.`is` import org.hamcrest.MatcherAssert.assertThat @@ -9,35 +10,36 @@ import java.util.* class FlyweightPatternShould { - @Before - fun initTests(){ - Flyweight.objectInstances = 0 - SoldierFactory.clearInstances() - } - @Test fun `Have one instance when only one type of soldier is created`() { - SoldierClient(Admiral.TYPE) - SoldierClient(Admiral.TYPE) - SoldierClient(Admiral.TYPE) + val soldiers = mutableListOf() + val soldiersAttack = SoldierClient(SoldierFactory(soldiers)) - assertThat(Flyweight.objectInstances, `is`(1)) + soldiersAttack.attack("Pedro", Point(1, 2)) + soldiersAttack.attack("Pedro", Point(2, 2)) + soldiersAttack.attack("Pedro", Point(3, 5)) + + assertThat(soldiers.filter { it.name == "Pedro" }.size, `is`(1)) } + @Test - fun `Have two instances when both types are created, no matter the number of soldiers`(){ - (1..Random().nextInt(30)).forEach { - SoldierClient(Admiral.TYPE) - SoldierClient(Captain.TYPE) + fun `Have two instances when both types are created, no matter the number of soldiers`() { + val soldiers = mutableListOf() + val soldierFactory = SoldierFactory(soldiers) + (1..1000).forEach { + soldierFactory.getSoldier("Pedro") + soldierFactory.getSoldier("Ryan") } - assertThat(Flyweight.objectInstances, `is`(2)) + assertThat(soldiers.size, `is`(2)) } @Test - fun `Have the same main object in two instances of the same type`(){ - val soldierOne: SoldierClient = SoldierClient(Admiral.TYPE) - val soldierTwo: SoldierClient = SoldierClient(Admiral.TYPE) + fun `Have the same main object in two instances of the same type`() { + val soldierFactory = SoldierFactory() + val soldierOne = soldierFactory.getSoldier("Pedro") + val soldierTwo = soldierFactory.getSoldier("Pedro") - assertThat(soldierOne.getSoldier(), `is`(soldierTwo.getSoldier())) + assertTrue(soldierOne === soldierTwo) } } From 9aa468f397a43afd0dccc547ddfe20aa50ac9334 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20L=C3=B3pez=20Mar=C3=ADn?= Date: Fri, 31 Mar 2017 13:43:50 +0200 Subject: [PATCH 4/5] Improving flyweight with more memory efficency --- src/main/kotlin/oop/Flyweight/Flyweight.kt | 14 +++++++------- src/main/kotlin/oop/Flyweight/Soldier.kt | 17 ++++++++++++++--- .../oop/Flyweigth/FlyweightPatternShould.kt | 18 +++++++++++++++--- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/oop/Flyweight/Flyweight.kt b/src/main/kotlin/oop/Flyweight/Flyweight.kt index 3ae6b9d..0d0e8bc 100644 --- a/src/main/kotlin/oop/Flyweight/Flyweight.kt +++ b/src/main/kotlin/oop/Flyweight/Flyweight.kt @@ -8,13 +8,13 @@ fun main(args: Array) { ) val soldiersAttacks = SoldierClient(SoldierFactory(soldiers)) - soldiersAttacks.attack("Pedro", Point(1,2)) - soldiersAttacks.attack("Ryan", Point(10,12)) - soldiersAttacks.attack("Ryan", Point(13,2)) - soldiersAttacks.attack("Forest", Point(133,233)) - soldiersAttacks.attack("Pedro", Point(1,1)) - soldiersAttacks.attack("Pedro", Point(2,2)) - soldiersAttacks.attack("Pedro", Point(44,5)) + soldiersAttacks.attack("Pedro", 1, 2) + soldiersAttacks.attack("Ryan", 10, 12) + soldiersAttacks.attack("Ryan", 13, 2) + soldiersAttacks.attack("Forest", 1, 2) + soldiersAttacks.attack("Pedro", 1, 2) + soldiersAttacks.attack("Pedro", 10, 12) + soldiersAttacks.attack("Pedro", 44, 5) soldiersAttacks.attacks.forEach(::println) } diff --git a/src/main/kotlin/oop/Flyweight/Soldier.kt b/src/main/kotlin/oop/Flyweight/Soldier.kt index 0ec70ba..b030e2a 100644 --- a/src/main/kotlin/oop/Flyweight/Soldier.kt +++ b/src/main/kotlin/oop/Flyweight/Soldier.kt @@ -4,7 +4,8 @@ data class Point(val x: Int, val y: Int) data class Soldier(val name: String) -class SoldierFactory(private val soldiers: MutableList = mutableListOf()) { +class SoldierFactory(private val soldiers: MutableList = mutableListOf(), + private val points: MutableList = mutableListOf()) { fun getSoldier(name: String): Soldier { if (!soldiers.map { it.name }.contains(name)) { @@ -14,6 +15,15 @@ class SoldierFactory(private val soldiers: MutableList = mutableListOf( return soldiers.filter { it.name == name }.first() } + fun getPoint(x: Int, y: Int): Point { + var point = Point(x, y) + if (!points.contains(point)) { + points.add(point) + } + + return points.filter { it == point }.first() + } + } data class Attack(val soldier: Soldier, val point: Point) { @@ -23,10 +33,11 @@ data class Attack(val soldier: Soldier, val point: Point) { } class SoldierClient(private val soldierFactory: SoldierFactory, - val attacks : MutableList = mutableListOf()) { + val attacks: MutableList = mutableListOf()) { - fun attack(soldierName: String, point: Point) { + fun attack(soldierName: String, x: Int, y: Int) { val soldier = soldierFactory.getSoldier(soldierName) + val point = soldierFactory.getPoint(x, y) attacks.add(Attack(soldier, point)) } diff --git a/src/test/kotlin/oop/Flyweigth/FlyweightPatternShould.kt b/src/test/kotlin/oop/Flyweigth/FlyweightPatternShould.kt index 118d357..ca85ae9 100644 --- a/src/test/kotlin/oop/Flyweigth/FlyweightPatternShould.kt +++ b/src/test/kotlin/oop/Flyweigth/FlyweightPatternShould.kt @@ -15,9 +15,9 @@ class FlyweightPatternShould { val soldiers = mutableListOf() val soldiersAttack = SoldierClient(SoldierFactory(soldiers)) - soldiersAttack.attack("Pedro", Point(1, 2)) - soldiersAttack.attack("Pedro", Point(2, 2)) - soldiersAttack.attack("Pedro", Point(3, 5)) + soldiersAttack.attack("Pedro", 1, 2) + soldiersAttack.attack("Pedro", 2, 2) + soldiersAttack.attack("Pedro", 2, 2) assertThat(soldiers.filter { it.name == "Pedro" }.size, `is`(1)) } @@ -42,4 +42,16 @@ class FlyweightPatternShould { assertTrue(soldierOne === soldierTwo) } + + @Test + fun `Have one instance of points when only one instance of point is created`() { + val points = mutableListOf() + val soldiersAttack = SoldierClient(SoldierFactory(points = points)) + + soldiersAttack.attack("Pedro", 3, 3) + soldiersAttack.attack("Forest", 3, 3) + soldiersAttack.attack("Forest", 3, 3) + + assertThat(points.size, `is`(1)) + } } From a4bfbb9c0b9289d483f464ef5361cbfe50438f6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20L=C3=B3pez=20Mar=C3=ADn?= Date: Fri, 31 Mar 2017 14:20:10 +0200 Subject: [PATCH 5/5] Created abstraction of Flyweight --- src/main/kotlin/oop/Flyweight/Soldier.kt | 29 ++++++++++--------- .../oop/Flyweigth/FlyweightPatternShould.kt | 18 ++++++------ 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/main/kotlin/oop/Flyweight/Soldier.kt b/src/main/kotlin/oop/Flyweight/Soldier.kt index b030e2a..69b8eae 100644 --- a/src/main/kotlin/oop/Flyweight/Soldier.kt +++ b/src/main/kotlin/oop/Flyweight/Soldier.kt @@ -4,24 +4,27 @@ data class Point(val x: Int, val y: Int) data class Soldier(val name: String) -class SoldierFactory(private val soldiers: MutableList = mutableListOf(), - private val points: MutableList = mutableListOf()) { +class Flyweight(val values: MutableSet = mutableSetOf()) { - fun getSoldier(name: String): Soldier { - if (!soldiers.map { it.name }.contains(name)) { - soldiers.add(Soldier(name)) - } + fun getFactory(value: A): A { + values.add(value) + return values.filter { it == value }.first() + } + +} - return soldiers.filter { it.name == name }.first() +class SoldierFactory(val soldiers: Flyweight = Flyweight(), + val points: Flyweight = Flyweight()) { + + constructor(soldiers: List = emptyList(), points: List = 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 { - var point = Point(x, y) - if (!points.contains(point)) { - points.add(point) - } - - return points.filter { it == point }.first() + return points.getFactory(Point(x, y)) } } diff --git a/src/test/kotlin/oop/Flyweigth/FlyweightPatternShould.kt b/src/test/kotlin/oop/Flyweigth/FlyweightPatternShould.kt index ca85ae9..374400c 100644 --- a/src/test/kotlin/oop/Flyweigth/FlyweightPatternShould.kt +++ b/src/test/kotlin/oop/Flyweigth/FlyweightPatternShould.kt @@ -12,31 +12,31 @@ class FlyweightPatternShould { @Test fun `Have one instance when only one type of soldier is created`() { - val soldiers = mutableListOf() - val soldiersAttack = SoldierClient(SoldierFactory(soldiers)) + val flyweight = Flyweight() + val soldiersAttack = SoldierClient(SoldierFactory(flyweight)) soldiersAttack.attack("Pedro", 1, 2) soldiersAttack.attack("Pedro", 2, 2) soldiersAttack.attack("Pedro", 2, 2) - assertThat(soldiers.filter { it.name == "Pedro" }.size, `is`(1)) + assertThat(flyweight.values.filter { it.name == "Pedro" }.size, `is`(1)) } @Test fun `Have two instances when both types are created, no matter the number of soldiers`() { - val soldiers = mutableListOf() - val soldierFactory = SoldierFactory(soldiers) + val flyweight = Flyweight() + val soldierFactory = SoldierFactory(flyweight) (1..1000).forEach { soldierFactory.getSoldier("Pedro") soldierFactory.getSoldier("Ryan") } - assertThat(soldiers.size, `is`(2)) + assertThat(flyweight.values.size, `is`(2)) } @Test fun `Have the same main object in two instances of the same type`() { - val soldierFactory = SoldierFactory() + val soldierFactory = SoldierFactory(Flyweight()) val soldierOne = soldierFactory.getSoldier("Pedro") val soldierTwo = soldierFactory.getSoldier("Pedro") @@ -45,13 +45,13 @@ class FlyweightPatternShould { @Test fun `Have one instance of points when only one instance of point is created`() { - val points = mutableListOf() + val points :Flyweight = Flyweight() val soldiersAttack = SoldierClient(SoldierFactory(points = points)) soldiersAttack.attack("Pedro", 3, 3) soldiersAttack.attack("Forest", 3, 3) soldiersAttack.attack("Forest", 3, 3) - assertThat(points.size, `is`(1)) + assertThat(points.values.size, `is`(1)) } }