-
Notifications
You must be signed in to change notification settings - Fork 1
Flyweight #28
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
base: master
Are you sure you want to change the base?
Flyweight #28
Changes from 2 commits
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 | ||
|
||
| } | ||
| init { | ||
| Flyweight.objectInstances++ | ||
|
||
| } | ||
|
|
||
| val attack = 6 | ||
|
||
| val salary = 23000 | ||
|
||
| 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 | ||
|
||
|
|
||
| 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) | ||
| } | ||
|
|
||
| } | ||
| 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) | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package oop.Flyweight | ||
|
|
||
| import java.awt.Point | ||
|
||
|
|
||
| interface Soldier { | ||
| fun attack(currentPosition: Point, attackPosition: Point) | ||
| } | ||
| 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 | ||
|
||
|
|
||
| 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 | ||
|
|
||
| } | ||
| 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 { | ||
|
||
| 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 | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } | ||
| 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())) | ||
| } | ||
| } |
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 ❌