Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flyweight #28

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/main/kotlin/oop/Flyweight/Admiral.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package oop.Flyweight

import java.awt.Point
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh my fucking god ❌


class Admiral : Soldier {
companion object {
val TYPE = 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOPE ❌

}
init {
Flyweight.objectInstances++
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dangerous ⚠️ If variable is public it can be overrided unsafely.

}

val attack = 6
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOPE ❌

val salary = 23000
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOPE ❌

override fun attack(currentPosition: Point, attackPosition: Point) {
currentPosition.move(attackPosition.x, attackPosition.y)
}

}
19 changes: 19 additions & 0 deletions src/main/kotlin/oop/Flyweight/Captain.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package oop.Flyweight

import java.awt.Point
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOPE ❌


class Captain : Soldier {
companion object {
val TYPE = 2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOPE ❌

}
init {
Flyweight.objectInstances++
}

val attack = 10
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOPE ❌

val salary = 10000
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOPE ❌ NOPE ❌ NOPE ❌

override fun attack(currentPosition: Point, attackPosition: Point) {
currentPosition.move(attackPosition.x, attackPosition.y)
}

}
26 changes: 26 additions & 0 deletions src/main/kotlin/oop/Flyweight/Flyweight.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package oop.Flyweight

import java.awt.Point
class Flyweight{
companion object{
var objectInstances = 0
}
}
fun main(args: Array<String>) {
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(Flyweight.objectInstances)

}
7 changes: 7 additions & 0 deletions src/main/kotlin/oop/Flyweight/Soldier.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package oop.Flyweight

import java.awt.Point
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOPE ❌ NOPE ❌ NOPE ❌ NOPE ❌ NOPE ❌


interface Soldier {
fun attack(currentPosition: Point, attackPosition: Point)
}
17 changes: 17 additions & 0 deletions src/main/kotlin/oop/Flyweight/SoldierClient.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package oop.Flyweight

import org.jetbrains.annotations.TestOnly
import java.awt.Point
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOL data class Point(val x: Int, val y:Int)


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

}
36 changes: 36 additions & 0 deletions src/main/kotlin/oop/Flyweight/SoldierFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package oop.Flyweight

import org.jetbrains.annotations.TestOnly

class SoldierFactory {
companion object {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this factory can be improved. It is so Java style...

var admiral: Admiral? = null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kotlin? null? seriously? ❌

var captain: Captain? = null

fun getSoldier(type: Int): Soldier{
Copy link
Member

@tonilopezmr tonilopezmr Mar 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getSoldier(type: Int) lol Do you know what is a sealed class? 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know*

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Cotel at least I can stop crying hard with this

when(type){
1 -> {
if(admiral == null){
admiral = Admiral()
}
return admiral!!
}
2 -> {
if(captain == null){
captain = Captain()
}
return captain!!
}
}
throw IllegalArgumentException()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xDDDDD fann1

}

@TestOnly
fun clearInstances(){
admiral = null
captain = null
}
}


}
43 changes: 43 additions & 0 deletions src/test/kotlin/oop/Flyweigth/FlyweightPatternShould.kt
Original file line number Diff line number Diff line change
@@ -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()))
}
}