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 1 commit
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 {
Flyweitght.objectInstances++
}

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 {
Flyweitght.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/Flyweitght.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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 Flyweitght{
Copy link
Member

Choose a reason for hiding this comment

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

Space between NOPE ❌ java.awt.Point and class

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(Flyweitght.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)
}
13 changes: 13 additions & 0 deletions src/main/kotlin/oop/Flyweight/SoldierClient.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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.

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)
}

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

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

}
}


}