-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: master
Are you sure you want to change the base?
Flyweight #28
Changes from 1 commit
1d9945d
f398035
b11a283
9aa468f
a4bfbb9
bb1e952
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package oop.Flyweight | ||
|
||
import java.awt.Point | ||
|
||
class Admiral : Soldier { | ||
companion object { | ||
val TYPE = 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NOPE ❌ |
||
} | ||
init { | ||
Flyweitght.objectInstances++ | ||
} | ||
|
||
val attack = 6 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NOPE ❌ |
||
val salary = 23000 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package oop.Flyweight | ||
|
||
import java.awt.Point | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NOPE ❌ |
||
|
||
class Captain : Soldier { | ||
companion object { | ||
val TYPE = 2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NOPE ❌ |
||
} | ||
init { | ||
Flyweitght.objectInstances++ | ||
} | ||
|
||
val attack = 10 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NOPE ❌ |
||
val salary = 10000 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package oop.Flyweight | ||
|
||
import java.awt.Point | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NOPE ❌ |
||
class Flyweitght{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Space between NOPE ❌ java.awt.Point and |
||
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) | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package oop.Flyweight | ||
|
||
import java.awt.Point | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package oop.Flyweight | ||
|
||
import java.awt.Point | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package oop.Flyweight | ||
|
||
class SoldierFactory { | ||
companion object { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. kotlin? null? seriously? ❌ |
||
var captain: Captain? = null | ||
|
||
fun getSoldier(type: Int): Soldier{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getSoldier(type: Int) lol Do you know what is a sealed class? 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you know* There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. xDDDDD fann1 |
||
} | ||
} | ||
|
||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh my fucking god ❌