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,7 @@
package com.example.algorithmstudy.pattern.behavoiral.command

class Button(private val command: Command) {
fun click() {
command.execute()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.algorithmstudy.pattern.behavoiral.command

/**
* @reference : https://chercher.tech/kotlin/command-design-pattern-kotlin
*/
interface Command {
fun execute()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.algorithmstudy.pattern.behavoiral.command

interface ConsumerWeapon {
fun fire()
fun reload()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.algorithmstudy.pattern.behavoiral.command

class CruiseMissile : ConsumerWeapon {
override fun fire() {
println("Firing Cruise missile")
}

override fun reload() {
println("Reloading Crise missile")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.algorithmstudy.pattern.behavoiral.command

class FireCommand(private val consumerWeapon: ConsumerWeapon): Command {
override fun execute() {
consumerWeapon.fire()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.algorithmstudy.pattern.behavoiral.command

class GrenadeLauncher : ConsumerWeapon {
override fun fire() {
println("Firing Grenade Launcher")
}

override fun reload() {
println("Reloading Grenade Launcher")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.algorithmstudy.pattern.behavoiral.command

class ReloadAllCommand(private val consumerWeaponList: List<ConsumerWeapon>) : Command {
override fun execute() {
consumerWeaponList.forEach {
it.reload()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.algorithmstudy.pattern.behavoiral.command

class UniversalFiringSystem {
fun getActiveWeapon(): ConsumerWeapon {
return CruiseMissile()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.algorithmstudy.pattern.behavoiral.state

object Complete : CookingStatus {
override val name = "Complete"

override fun foward(cooking: Cooking) = "다음 상태 없음"

override fun backward(cooking: Cooking): String {
cooking.status = OnCook
return cooking.status.name
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.algorithmstudy.pattern.behavoiral.state

class Cooking {
var status: CookingStatus = Wait

fun fowardStatus() = status.foward(this)
fun backwardStatus() = status.backward(this)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.algorithmstudy.pattern.behavoiral.state

interface CookingStatus {
val name: String
fun foward(cooking: Cooking): String
fun backward(cooking: Cooking): String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.algorithmstudy.pattern.behavoiral.state

object OnCook : CookingStatus {
override val name = "OnCook"

override fun foward(cooking: Cooking): String {
cooking.status = Complete
return cooking.status.name
}

override fun backward(cooking: Cooking): String {
cooking.status = Preparing
return cooking.status.name
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.algorithmstudy.pattern.behavoiral.state

object Preparing : CookingStatus {
override val name = "Preparing"

override fun foward(cooking: Cooking): String {
cooking.status = OnCook
return cooking.status.name
}

override fun backward(cooking: Cooking): String {
cooking.status = Wait
return cooking.status.name
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.algorithmstudy.pattern.behavoiral.state

object Wait : CookingStatus {
override val name = "Waiting"

override fun foward(cooking: Cooking): String {
cooking.status = Preparing
return cooking.status.name
}

override fun backward(cooking: Cooking): String {
return "이전 상태 없음"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.algorithmstudy.pattern.behavoiral.visitor

class Euljiro : Region {
override fun accept(visitor: RegionVisitor) = visitor.visit(region = this)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.algorithmstudy.pattern.behavoiral.visitor

class Gangnam : Region {
override fun accept(visitor: RegionVisitor) = visitor.visit(region = this)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.algorithmstudy.pattern.behavoiral.visitor

class Hongdae : Region {
override fun accept(visitor: RegionVisitor) = visitor.visit(region = this)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.algorithmstudy.pattern.behavoiral.visitor

interface Region {
fun accept(visitor: RegionVisitor)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.algorithmstudy.pattern.behavoiral.visitor

interface RegionVisitor {
fun visit(region: Region)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.algorithmstudy.pattern.behavoiral.visitor

class Traveler : RegionVisitor {
override fun visit(region: Region) {
println("Visiting $region. It\'s so good!")
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.algorithmstudy.pattern.creation.singleton

/**
* ref : https://stackoverflow.com/questions/56825097/synchronized-singleton-in-kotlin
* @ref : https://stackoverflow.com/questions/56825097/synchronized-singleton-in-kotlin
*/
class CustomSingleton private constructor() {
companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.algorithmstudy.pattern.structure.decorator

/**
* reference : https://www.baeldung.com/kotlin/decorator-pattern
* @reference : https://www.baeldung.com/kotlin/decorator-pattern
*/
interface ChristmasTree {
fun decorate(): String
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.example.algorithmstudy.design_pattern

import com.example.algorithmstudy.pattern.behavoiral.command.*
import org.junit.Test

/**
* Command Pattern
*
* - 커맨터 패턴은 Command interface, invoker class, receiver class 로 구성된다.
* - Command Interface는 execute function을 갖는다.
* - Receiver에는 실제 수행해야 할 기능을 정의하며, invoker class가 Receiver의 function을 invoke 하는 역할을 맡게 된다.
*
*/
class CommandTest {

@Test
fun testCommand() {
// ConsumerWeapon의 구현체 : Receiver
// button: Invoker

val cw = UniversalFiringSystem().getActiveWeapon()
val fireCommand = FireCommand(cw)
val button = Button(fireCommand)
button.click()

val weaponList = listOf(CruiseMissile(), GrenadeLauncher())
val reloadAllCommand = ReloadAllCommand(weaponList)
val reloadButton = Button(reloadAllCommand)
reloadButton.click()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.example.algorithmstudy.design_pattern

import com.example.algorithmstudy.pattern.behavoiral.state.Cooking
import org.junit.Test

/**
* State Pattern
*
* @reference : https://kimchanjung.github.io/design-pattern/2020/05/26/state-pattern/
*
* - 각 상태를 단계별로 체이닝해 정의함으로써 특정 개념의 전후 상태를 명확하게 정의하고 관리할 수 있음.
* - Client는 새로운 상태의 추가에 구애받지 않고 기존의 로직을 유지 가능
*/
class StateTest {

@Test
fun stateTest() {
val cooking = Cooking()

println(cooking.status)
println(cooking.fowardStatus())
println(cooking.fowardStatus())
println(cooking.fowardStatus())
println(cooking.fowardStatus())
println(cooking.backwardStatus())
println(cooking.backwardStatus())
println(cooking.backwardStatus())
println(cooking.backwardStatus())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.algorithmstudy.design_pattern

import com.example.algorithmstudy.pattern.behavoiral.visitor.Euljiro
import com.example.algorithmstudy.pattern.behavoiral.visitor.Gangnam
import com.example.algorithmstudy.pattern.behavoiral.visitor.Hongdae
import com.example.algorithmstudy.pattern.behavoiral.visitor.Traveler
import org.junit.Test

/**
* Visitor Pattern
*
* @reference : https://chercher.tech/kotlin/visitor-design-pattern-kotlin
*
* - Visitor pattern을 이용하면, 어느 클래스에서 수행하는 로직을 다른 클래스로 이전해 사용할 수 있게 된다.
* - 즉, 아래의 테스트에서는 각 Region에서 수행하고자 하는 로직을 Traveler 클래스 안에 정의하고 실행하도록 하는 것이다.
*/
class VisitorTest {

@Test
fun visitorTest() {
val region = listOf(Gangnam(), Hongdae(), Euljiro())
val traveler = Traveler()
region.forEach {
it.accept(traveler)
}
}
}