-
Notifications
You must be signed in to change notification settings - Fork 415
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
base: relkimm
Are you sure you want to change the base?
5단계 - 자동차 경주(리팩터링) #918
Changes from all commits
41be7a3
32edb84
3392656
ffe32fd
ccb8ca3
28acace
ee967ec
51ab1c0
b31626d
e8b33e2
81e58af
459562a
b4497de
a0bb3ab
6aed63f
3e6350e
133c53c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
package racingcar | ||
package racingcar.domain | ||
|
||
class CarGroup(private val cars: List<Car>) { | ||
class CarGroup(private var cars: List<Car>) { | ||
fun move() { | ||
this.cars.forEach { car -> | ||
this.cars = this.cars.map { car -> | ||
val oil = OilStation.generateOilRandomly() | ||
car.move(movePolicy = OilPolicy(oil = oil)) | ||
} | ||
Comment on lines
+3
to
8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CarGroup 이 move 할 때도 immutable 하게 cars 를 반환하는 것이 좀 더 안전한 프로그램 아닐까 싶은데요~ 🤔 |
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,15 @@ | ||||||
package racingcar.domain | ||||||
|
||||||
@JvmInline | ||||||
value class CarName( | ||||||
val value: String, | ||||||
) { | ||||||
init { | ||||||
require(this.value.isNullOrBlank().not()) { "자동차 이름은 비어 있을 수 없습니다." } | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 마이너하지만
Suggested change
|
||||||
require(this.value.length <= MAX_NAME_LENGTH) { "자동차 이름은 5 글자를 초과할 수 없습니다." } | ||||||
} | ||||||
Comment on lines
+4
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 자동차 이름이 비어있을 경우의 유효성 검사도 추가해 보았어요~ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 꼼꼼하게 피드백 반영해주셨네요~ 👍 |
||||||
|
||||||
companion object { | ||||||
private const val MAX_NAME_LENGTH = 5 | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package racingcar | ||
package racingcar.domain | ||
|
||
object CarNameSplitter { | ||
private const val CAR_NAME_DELIMITER = "," | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package racingcar | ||
package racingcar.domain | ||
|
||
data class CarPosition private constructor( | ||
val name: CarName, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package racingcar | ||
package racingcar.domain | ||
|
||
interface MovePolicy { | ||
fun canMove(): Boolean | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package racingcar | ||
package racingcar.domain | ||
|
||
@JvmInline | ||
value class Oil(val amount: Int) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package racingcar | ||
package racingcar.domain | ||
|
||
class OilPolicy( | ||
private val oil: Oil, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package racingcar | ||
package racingcar.domain | ||
|
||
object OilStation { | ||
private val RANDOM_RANGE = (0..9) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package racingcar | ||
package racingcar.domain | ||
|
||
@JvmInline | ||
value class Position(val value: Int) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package racingcar.domain | ||
|
||
class Round(val id: Int) { | ||
fun start(carGroup: CarGroup) { | ||
carGroup.move() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package racingcar | ||
package racingcar.domain | ||
|
||
object RoundFactory { | ||
fun createMany(amount: Int): List<Round> { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package racingcar | ||
package racingcar.domain | ||
|
||
data class Winner(val name: CarName) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package racingcar | ||
package racingcar.domain | ||
|
||
object WinnerCalculator { | ||
fun execute(cars: List<Car>): List<Car> { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package racingcar.domain.dto | ||
|
||
import racingcar.domain.CarName | ||
|
||
data class CarCreateDto( | ||
val name: CarName, | ||
) |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package racingcar.view.component | ||
|
||
import racingcar.domain.Position | ||
import racingcar.view.ui.Span | ||
|
||
class CarDistanceComponent( | ||
private val position: Position, | ||
) : Component { | ||
override fun render() { | ||
repeat(this.position.value) { Span(text = "-").draw() } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package racingcar.view.component | ||
|
||
import racingcar.view.ui.Span | ||
|
||
class CarNameComponent( | ||
private val name: String, | ||
) : Component { | ||
override fun render() { | ||
Span(text = "${this.name} : ").draw() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package racingcar.view.component | ||
|
||
import racingcar.domain.CarPosition | ||
import racingcar.view.ui.Br | ||
|
||
class CarPositionComponent(private val carPositions: List<CarPosition>) : Component { | ||
override fun render() { | ||
this.carPositions.forEach { carPosition -> | ||
CarNameComponent(name = carPosition.name.value).render() | ||
CarDistanceComponent(position = carPosition.position).render() | ||
Br().draw() | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package racingcar.component | ||
package racingcar.view.component | ||
|
||
interface Component { | ||
fun render() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package racingcar.view.component | ||
|
||
import racingcar.view.container.RoundListContainer | ||
import racingcar.view.container.WinnerContainer | ||
|
||
class RacingResultComponent : Component { | ||
override fun render() { | ||
RoundListContainer().render() | ||
WinnerContainer().render() | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
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.
immutable
하게 만들어 보았습니다~🙆♂️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.
새로운 car 객체를 잘 생성해주신 거 같은데요~
position 프로퍼티를 불변(val)으로 가지고 있어야 진정한 불변 객체가 아닐까 싶습니다! 🤔
현재는 car 객체의 position 값이 var 로 선언되어있네요~