-
Notifications
You must be signed in to change notification settings - Fork 410
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
[step5] 자동차 경주(리팩토링) #1556
base: mkkim90
Are you sure you want to change the base?
[step5] 자동차 경주(리팩토링) #1556
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
package racingcar.domain | ||
|
||
import racingcar.dto.CarDto | ||
import racingcar.dto.CarsDto | ||
|
||
object WinnerCar { | ||
fun from(racingCars: Cars): List<Car> { | ||
return racingCars.cars | ||
.filter { it.position == racingCars.maxOfPositions() } | ||
fun from(racingCars: CarsDto): List<CarDto> { | ||
return racingCars.contentsFilter { it -> it.position == racingCars.maxOfPositions() } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package racingcar.dto | ||
|
||
import racingcar.domain.Car | ||
|
||
data class CarDto(val name: String, val position: Int) { | ||
companion object { | ||
fun from(car: Car): CarDto { | ||
return CarDto( | ||
name = car.name, | ||
position = car.position | ||
) | ||
} | ||
} | ||
} | ||
|
||
data class CarsDto(val carsDto: List<CarDto>) { | ||
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. DTO의 의미는 data transfer object입니다. 즉 데이터 전송용 바스켓 같은 목적으로 사용되죠. |
||
fun racingTrace() = buildString { | ||
carsDto.forEach { | ||
appendLine("${it.name} : ${RACING_COURSE.repeat(maxOf(0, it.position))}") | ||
} | ||
} | ||
|
||
fun contentsFilter(predicate: (CarDto) -> Boolean): List<CarDto> { | ||
return carsDto.filter(predicate) | ||
} | ||
|
||
fun maxOfPositions(): Int { | ||
return carsDto.maxOf { car -> car.position } | ||
} | ||
|
||
companion object { | ||
private const val RACING_COURSE = "-" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package racingcar.service | ||
|
||
import racingcar.domain.Cars | ||
import racingcar.dto.CarsDto | ||
import racingcar.view.inputTryCount | ||
|
||
class RacingService { | ||
fun play(racingCars: Cars): List<CarsDto> { | ||
val snapshots = mutableListOf<CarsDto>() | ||
repeat(inputTryCount()) { | ||
racingCars.move() | ||
snapshots.add(CarsDto(racingCars.toCarDtoList())) | ||
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. 서비스에서 dto를 반환해도 되는가? 다른 서비스에서 같은 로직을 수행하는데 dto가 아닌 domain을 받고 싶다면 어떻게 하지? 이런 고민을 해보면 좋은 경험이 될 것 같아요 ㅎㅎ (정답이 있거나 이게 틀렸다는 피드백이 아닙니다 ㅎㅎ) |
||
} | ||
return snapshots | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package racingcar | ||
package racingcar.view | ||
|
||
fun inputTryCount(): Int { | ||
println("시도할 횟수는 몇 회인가요?") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,24 @@ | ||
package racingcar.view | ||
|
||
import racingcar.domain.Car | ||
import racingcar.domain.Cars | ||
import racingcar.dto.CarDto | ||
import racingcar.dto.CarsDto | ||
|
||
fun printRacing(racingCars: Cars) { | ||
racingCars.cars.forEach { | ||
println("${it.name} : ${"-".repeat(maxOf(0, it.position))}") | ||
object OutputView { | ||
|
||
private const val WINNER_JOIN_SEPARATOR = "," | ||
fun printRacing(snapShots: List<CarsDto>) { | ||
val result = buildString { | ||
snapShots.forEach { | ||
appendLine(it.racingTrace()) | ||
} | ||
} | ||
println(result) | ||
} | ||
println() | ||
} | ||
|
||
fun printWinner(winnerCars: List<Car>): String = buildString { | ||
val winnersCarNames = winnerCars | ||
.joinToString(",") { it.name } | ||
fun printWinners(winnerCars: List<CarDto>): String = buildString { | ||
val winnersCarNames = winnerCars | ||
.joinToString(WINNER_JOIN_SEPARATOR) { it.name } | ||
|
||
println("${winnersCarNames}가 최종 우승했습니다.") | ||
println("${winnersCarNames}가 최종 우승했습니다.") | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package racingcar | ||
|
||
import io.kotest.matchers.shouldBe | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.ValueSource | ||
import racingcar.domain.Cars | ||
|
||
class CarsTest { | ||
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. 테스트가 성공하지 않고 있네요. ㅠ |
||
@ValueSource(strings = ["test,123,ㅅㄷㅈ", "민균,한솔,재성"]) | ||
@ParameterizedTest | ||
fun `각 자동차 이름을 구분자(,)로 구분한다`(param: String) { | ||
val cars = Cars.of(param) | ||
cars.cars shouldBe param.split(",") | ||
} | ||
} |
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.
보통 정적 팩토리 함수에서 매개변수를 하나만 받는 경우에는 from이나 valueOf등을 사용하는걸 관례로 하고 있습니다.
참고: https://kyucumber.github.io/posts/book/effective-kotlin/effective-kotlin-item-33