-
Notifications
You must be signed in to change notification settings - Fork 412
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 1 commit
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package racingcar.service | ||
|
||
import racingcar.domain.Cars | ||
import racingcar.view.OutputView | ||
import racingcar.view.inputTryCount | ||
|
||
class RacingService { | ||
fun play(racingCars: Cars): Cars { | ||
repeat(inputTryCount()) { | ||
racingCars.move() | ||
OutputView.printRacing(racingCars) | ||
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. 지금 구조를 보면 의존성의 흐름이 Controller -> Service -> View Layer입니다. 이런 출력 계층에 대한 정보는 컨트롤러 이상이 알 필요가 없도록 고쳐보면 좋을 것 같아요 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. controller 와 service 간에 레이싱 스냅샷 데이터를 출력할 수 있는 DTO 를 추가했습니다 |
||
} | ||
return racingCars | ||
} | ||
} |
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 |
---|---|---|
|
@@ -3,16 +3,24 @@ package racingcar.view | |
import racingcar.domain.Car | ||
import racingcar.domain.Cars | ||
|
||
fun printRacing(racingCars: Cars) { | ||
racingCars.cars.forEach { | ||
println("${it.name} : ${"-".repeat(maxOf(0, it.position))}") | ||
object OutputView { | ||
|
||
private const val WINNER_JOIN_SEPARATOR = "," | ||
private const val RACING_COURSE = "-" | ||
fun printRacing(racingCars: Cars) { | ||
val result = buildString { | ||
racingCars.cars.forEach { | ||
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. a.b.c() 현재 a라는 객체에서 c라는 함수호출에 대해 알 필요가 있을까요? 디미터 법칙에 위배됩니다. racingCar.contentForEach { ... } 이런식으로 함수를 만들어서 제공해도 좋을 것 같아요. 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에게 출력 문자열을 위임했습니다. |
||
appendLine("${it.name} : ${RACING_COURSE.repeat(maxOf(0, it.position))}") | ||
} | ||
appendLine() | ||
} | ||
println(result) | ||
} | ||
println() | ||
} | ||
|
||
fun printWinner(winnerCars: List<Car>): String = buildString { | ||
val winnersCarNames = winnerCars | ||
.joinToString(",") { it.name } | ||
fun printWinners(winnerCars: List<Car>): String = buildString { | ||
val winnersCarNames = winnerCars | ||
.joinToString(WINNER_JOIN_SEPARATOR) { it.name } | ||
|
||
println("${winnersCarNames}가 최종 우승했습니다.") | ||
println("${winnersCarNames}가 최종 우승했습니다.") | ||
} | ||
} |
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.
자동차 이름을 입력받은 것을
컨트롤러
계층에서 분리하고 있네요. 즉 비즈니스 로직이 컨트롤러에서 수행되는 것인데, 이 경우 자동차 이름을 정상적으로 분리를 하는지에 대한 테스트를 하기 힘들 것 같아요. 🤔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.
Cars 클래스에 위임하겠습니다.