Skip to content

5단계 - 자동차 경주(리팩터링) #918

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

Open
wants to merge 17 commits into
base: relkimm
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
41be7a3
refactor: 패키지를 domain & view 로 분리
relkimm Nov 21, 2022
32edb84
refactor: CarName 글자수 검사 시 require 사용하도록 변경
relkimm Nov 21, 2022
3392656
feat: 기존 자동차를 통해 새로운 자동차 생성하는 로직 추가
relkimm Nov 21, 2022
ffe32fd
refactor: 자동차가 주행하고 나서 새로운 자동차를 반환하도록 수정
relkimm Nov 21, 2022
ccb8ca3
refactor: CarGroup 에서 자동차가 주행하더라도 불변하도록 수정
relkimm Nov 21, 2022
28acace
refactor: Store pub/sub 구조로 변경
relkimm Nov 21, 2022
ee967ec
feat: 라운드 관련 UI 를 표시하는 RoundComponent 추가
relkimm Nov 21, 2022
51ab1c0
refactor: Winner 를 표시하는 UI container/presentational 컴포넌트로 분리
relkimm Nov 21, 2022
b31626d
refactor: Round 를 표시하는 UI container/presentational 컴포넌트로 분리
relkimm Nov 21, 2022
e8b33e2
feat: 자동차 위치를 표시하는 UI container/presentational 컴포넌트 추가
relkimm Nov 21, 2022
81e58af
refactor: 불필요한 RoundResult 정리
relkimm Nov 21, 2022
459562a
refactor: RoundList 와 Round 를 표시하는 UI 분리
relkimm Nov 21, 2022
b4497de
refactor: DistanceComponent 네이밍 변경
relkimm Nov 21, 2022
a0bb3ab
refactor: 프로퍼티에는 this 붙이도록 수정
relkimm Nov 21, 2022
6aed63f
refactor: RoundContainer 에서 라운드 시작하는 로직 render 와 분리
relkimm Nov 21, 2022
3e6350e
feat: 자동차 이름 공백인 경우 유효성 검사 추가
relkimm Nov 21, 2022
133c53c
refactor: store 공통으로 사용할 수 있도록 개선
relkimm Nov 21, 2022
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
18 changes: 1 addition & 17 deletions src/main/kotlin/racingcar/view/store/CarGroupStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,4 @@ package racingcar.view.store

import racingcar.domain.CarGroup

object CarGroupStore : Store<CarGroup>() {
private var carGroup = CarGroup(cars = listOf())

override fun getState(): CarGroup {
return this.carGroup
}

override fun setState(state: CarGroup) {
this.carGroup = state
this.listeners.forEach { listener -> listener() }
}

override fun subscribe(listener: Listener): UnSubscribe {
this.listeners.add(listener)
return { this.listeners.remove(listener) }
}
}
object CarGroupStore : Store<CarGroup>(initialState = CarGroup(cars = listOf()))
18 changes: 1 addition & 17 deletions src/main/kotlin/racingcar/view/store/RoundStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,4 @@ package racingcar.view.store

import racingcar.domain.Round

object RoundStore : Store<List<Round>>() {
private var rounds = listOf<Round>()

override fun getState(): List<Round> {
return this.rounds
}

override fun setState(state: List<Round>) {
this.rounds = state
this.listeners.forEach { listener -> listener() }
}

override fun subscribe(listener: Listener): UnSubscribe {
this.listeners.add(listener)
return { this.listeners.remove(listener) }
}
}
object RoundStore : Store<List<Round>>(initialState = listOf())
19 changes: 14 additions & 5 deletions src/main/kotlin/racingcar/view/store/Store.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@ package racingcar.view.store
typealias Listener = () -> Unit
typealias UnSubscribe = () -> Unit

abstract class Store<T> {
protected val listeners = HashSet<Listener>()
open class Store<T>(initialState: T) {
private var state = initialState
private val listeners = HashSet<Listener>()

abstract fun getState(): T
abstract fun setState(state: T)
abstract fun subscribe(listener: Listener): UnSubscribe
fun getState(): T {
return this.state
}
fun setState(state: T) {
this.state = state
this.listeners.forEach { listener -> listener() }
}
fun subscribe(listener: Listener): UnSubscribe {
listeners.add(listener)
return { listeners.remove(listener) }
}
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전역 상태 관리할 수 있는 store 를 공통으로 사용하기 위해 요렇게 개선해 보았습니다~🙆‍♂️

18 changes: 1 addition & 17 deletions src/test/kotlin/racingcar/view/store/StoreTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,4 @@ class StoreTest : FunSpec({
}
})

object CarNameStore : Store<CarName>() {
private var carName = CarName(value = "")

override fun getState(): CarName {
return this.carName
}

override fun subscribe(listener: Listener): UnSubscribe {
this.listeners.add(listener)
return { listeners.remove(listener) }
}

override fun setState(state: CarName) {
this.carName = state
listeners.forEach { listener -> listener() }
}
}
object CarNameStore : Store<CarName>(initialState = CarName(value = "동구"))