diff --git a/app/src/main/java/com/example/algorithmstudy/pattern/structure/composite/InfantrySquad.kt b/app/src/main/java/com/example/algorithmstudy/pattern/structure/composite/InfantrySquad.kt new file mode 100644 index 0000000..6fd0bd1 --- /dev/null +++ b/app/src/main/java/com/example/algorithmstudy/pattern/structure/composite/InfantrySquad.kt @@ -0,0 +1,10 @@ +package com.example.algorithmstudy.pattern.structure.composite + +class InfantrySquad(val units: MutableList = mutableListOf()) { + + constructor(vararg units: InfantryUnit) : this(mutableListOf()) { + units.forEach { + this.units.add(it) + } + } +} diff --git a/app/src/main/java/com/example/algorithmstudy/pattern/structure/composite/InfantryUnit.kt b/app/src/main/java/com/example/algorithmstudy/pattern/structure/composite/InfantryUnit.kt new file mode 100644 index 0000000..1d1ca11 --- /dev/null +++ b/app/src/main/java/com/example/algorithmstudy/pattern/structure/composite/InfantryUnit.kt @@ -0,0 +1,4 @@ +package com.example.algorithmstudy.pattern.structure.composite + +interface InfantryUnit { +} diff --git a/app/src/main/java/com/example/algorithmstudy/pattern/structure/composite/Rifleman.kt b/app/src/main/java/com/example/algorithmstudy/pattern/structure/composite/Rifleman.kt new file mode 100644 index 0000000..6362151 --- /dev/null +++ b/app/src/main/java/com/example/algorithmstudy/pattern/structure/composite/Rifleman.kt @@ -0,0 +1,4 @@ +package com.example.algorithmstudy.pattern.structure.composite + +class Rifleman: InfantryUnit { +} diff --git a/app/src/main/java/com/example/algorithmstudy/pattern/structure/composite/RocketSoldier.kt b/app/src/main/java/com/example/algorithmstudy/pattern/structure/composite/RocketSoldier.kt new file mode 100644 index 0000000..c4036aa --- /dev/null +++ b/app/src/main/java/com/example/algorithmstudy/pattern/structure/composite/RocketSoldier.kt @@ -0,0 +1,4 @@ +package com.example.algorithmstudy.pattern.structure.composite + +class RocketSoldier : InfantryUnit { +} diff --git a/app/src/main/java/com/example/algorithmstudy/pattern/structure/composite/Sniper.kt b/app/src/main/java/com/example/algorithmstudy/pattern/structure/composite/Sniper.kt new file mode 100644 index 0000000..16c36f6 --- /dev/null +++ b/app/src/main/java/com/example/algorithmstudy/pattern/structure/composite/Sniper.kt @@ -0,0 +1,4 @@ +package com.example.algorithmstudy.pattern.structure.composite + +class Sniper : InfantryUnit { +} diff --git a/app/src/main/java/com/example/algorithmstudy/pattern/structure/flyweight/Minigun.kt b/app/src/main/java/com/example/algorithmstudy/pattern/structure/flyweight/Minigun.kt new file mode 100644 index 0000000..d5df7d2 --- /dev/null +++ b/app/src/main/java/com/example/algorithmstudy/pattern/structure/flyweight/Minigun.kt @@ -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") + } +} diff --git a/app/src/main/java/com/example/algorithmstudy/pattern/structure/flyweight/Shotgun.kt b/app/src/main/java/com/example/algorithmstudy/pattern/structure/flyweight/Shotgun.kt new file mode 100644 index 0000000..1301dbe --- /dev/null +++ b/app/src/main/java/com/example/algorithmstudy/pattern/structure/flyweight/Shotgun.kt @@ -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") + } +} diff --git a/app/src/main/java/com/example/algorithmstudy/pattern/structure/flyweight/Weapon.kt b/app/src/main/java/com/example/algorithmstudy/pattern/structure/flyweight/Weapon.kt new file mode 100644 index 0000000..da8e919 --- /dev/null +++ b/app/src/main/java/com/example/algorithmstudy/pattern/structure/flyweight/Weapon.kt @@ -0,0 +1,7 @@ +package com.example.algorithmstudy.pattern.structure.flyweight + +interface Weapon { + fun fire() + fun reload() + fun fix() +} diff --git a/app/src/main/java/com/example/algorithmstudy/pattern/structure/flyweight/WeaponFactory.kt b/app/src/main/java/com/example/algorithmstudy/pattern/structure/flyweight/WeaponFactory.kt new file mode 100644 index 0000000..ea4f6f1 --- /dev/null +++ b/app/src/main/java/com/example/algorithmstudy/pattern/structure/flyweight/WeaponFactory.kt @@ -0,0 +1,25 @@ +package com.example.algorithmstudy.pattern.structure.flyweight + +import android.annotation.SuppressLint + +class WeaponFactory { + private val weaponMap = mutableMapOf() + + @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" + } +} diff --git a/app/src/test/java/com/example/algorithmstudy/design_pattern/CompositeTest.kt b/app/src/test/java/com/example/algorithmstudy/design_pattern/CompositeTest.kt new file mode 100644 index 0000000..3c6ee80 --- /dev/null +++ b/app/src/test/java/com/example/algorithmstudy/design_pattern/CompositeTest.kt @@ -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) + } +} diff --git a/app/src/test/java/com/example/algorithmstudy/design_pattern/FlyweightTest.kt b/app/src/test/java/com/example/algorithmstudy/design_pattern/FlyweightTest.kt new file mode 100644 index 0000000..355726f --- /dev/null +++ b/app/src/test/java/com/example/algorithmstudy/design_pattern/FlyweightTest.kt @@ -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)) + } +}