Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.algorithmstudy.pattern.structure.composite

class InfantrySquad(val units: MutableList<InfantryUnit> = mutableListOf()) {

constructor(vararg units: InfantryUnit) : this(mutableListOf()) {
units.forEach {
this.units.add(it)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.example.algorithmstudy.pattern.structure.composite

interface InfantryUnit {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.example.algorithmstudy.pattern.structure.composite

class Rifleman: InfantryUnit {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.example.algorithmstudy.pattern.structure.composite

class RocketSoldier : InfantryUnit {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.example.algorithmstudy.pattern.structure.composite

class Sniper : InfantryUnit {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.algorithmstudy.pattern.structure.flyweight

class Minigun : Weapon {
override fun fire() {
println("fire $this")
}

override fun reload() {
println("reload $this")
}

override fun fix() {
println("fix $this")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.algorithmstudy.pattern.structure.flyweight

class Shotgun : Weapon {
override fun fire() {
println("fire $this")
}

override fun reload() {
println("reload $this")
}

override fun fix() {
println("fix $this")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.algorithmstudy.pattern.structure.flyweight

interface Weapon {
fun fire()
fun reload()
fun fix()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.example.algorithmstudy.pattern.structure.flyweight

import android.annotation.SuppressLint

class WeaponFactory {
private val weaponMap = mutableMapOf<String, Weapon?>()

@SuppressLint("NewApi")
fun getWeapon(type: String): Weapon? {
if (weaponMap[type] == null) {
weaponMap[type] = when (type) {
WEAPON_SHOTGUN -> Shotgun()
WEAPON_MINIGUN -> Minigun()
else -> null
}
}

return weaponMap[type]
}

companion object {
const val WEAPON_SHOTGUN = "shotgun"
const val WEAPON_MINIGUN = "minigun"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.algorithmstudy.design_pattern

import com.example.algorithmstudy.pattern.structure.composite.InfantrySquad
import com.example.algorithmstudy.pattern.structure.composite.Rifleman
import com.example.algorithmstudy.pattern.structure.composite.RocketSoldier
import com.example.algorithmstudy.pattern.structure.composite.Sniper
import org.junit.Test

/**
* Composite Pattern
*
* - 유사한 클래스의 일반화하여 통합 관리할 수 있는 패턴
*/
class CompositeTest {

@Test
fun testComposite() {
val squad = InfantrySquad(Rifleman(), Rifleman(), Sniper(), RocketSoldier())

println(squad.units)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.algorithmstudy.design_pattern

import com.example.algorithmstudy.pattern.creation.prototype.MyComputer
import com.example.algorithmstudy.pattern.structure.flyweight.WeaponFactory
import org.junit.Test

/**
* Flyweight Pattern
*
* - 단일 객체 생성으로 메모리 효율을 높이는 패턴
*/
class FlyweightTest {

@Test
fun testFlyweight() {
val factory = WeaponFactory()

println(factory.getWeapon(WeaponFactory.WEAPON_MINIGUN))
println(factory.getWeapon(WeaponFactory.WEAPON_MINIGUN))
println(factory.getWeapon(WeaponFactory.WEAPON_SHOTGUN))
println(factory.getWeapon(WeaponFactory.WEAPON_SHOTGUN))
}
}