-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Step 5 - 자동차경주(리팩토링) #5815
base: sparcsjara
Are you sure you want to change the base?
Step 5 - 자동차경주(리팩토링) #5815
Changes from all commits
7979eeb
cb61665
3b86c47
e84f9f9
a1cc5e1
473befd
8e9473b
edd4f72
7f598ab
b738591
61e0dc8
0b77a40
0186187
531142a
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,39 +1,33 @@ | ||||||
package racingcar.domain; | ||||||
|
||||||
import racingcar.domain.movableStrategy.RandomMovableStrategy; | ||||||
import racingcar.dto.RacingCarStatesDTO; | ||||||
import racingcar.dto.RacingWrapResultDTO; | ||||||
import racingcar.dto.RacingWrapResultsDTO; | ||||||
import racingcar.domain.movableStrategy.MovableStrategy; | ||||||
|
||||||
import java.util.ArrayList; | ||||||
import java.util.Collections; | ||||||
import java.util.Comparator; | ||||||
import java.util.List; | ||||||
import java.util.Objects; | ||||||
|
||||||
public class CarRacing { | ||||||
private final RacingFleet racingFleet; | ||||||
private final RacingWrapResultsDTO wrapResults; | ||||||
private final RacingHistories histories; | ||||||
private final MovableStrategy movableStrategy; | ||||||
Comment on lines
+10
to
+11
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. histories 객체는 proceedWraps 메서드의 결과인데요. movablesStrategy 변수는 racingFleet.raceAll 메서드에만 필요합니다. |
||||||
|
||||||
private CarRacing(RacingFleet racingFleet, RacingWrapResultsDTO wrapResults) { | ||||||
private CarRacing(RacingFleet racingFleet, RacingHistories histories, MovableStrategy movableStrategy) { | ||||||
this.racingFleet = racingFleet; | ||||||
this.wrapResults = wrapResults; | ||||||
this.histories = histories; | ||||||
this.movableStrategy = movableStrategy; | ||||||
} | ||||||
|
||||||
public static CarRacing valueOf(List<String> carNames) { | ||||||
public static CarRacing valueOf(List<String> carNames, MovableStrategy movableStrategy) { | ||||||
RacingFleet racingFleet = initializeRacingCars(carNames); | ||||||
List<RacingWrapResultDTO> wrapResults = initializeWrapResults(racingFleet); | ||||||
return new CarRacing(racingFleet, RacingWrapResultsDTO.valueOf(wrapResults)); | ||||||
RacingHistories histories = initializeRacingHistories(racingFleet); | ||||||
return new CarRacing(racingFleet, histories, movableStrategy); | ||||||
} | ||||||
|
||||||
private static RacingFleet initializeRacingCars(List<String> carNames) { | ||||||
return RacingFleet.valueOf(carNames); | ||||||
} | ||||||
|
||||||
private static List<RacingWrapResultDTO> initializeWrapResults(RacingFleet racingFleet) { | ||||||
List<RacingWrapResultDTO> wrapResults = new ArrayList<>(); | ||||||
wrapResults.add(RacingWrapResultDTO.valueOf(0, RacingCarStatesDTO.valueOf(racingFleet.getRacingCars()))); | ||||||
return wrapResults; | ||||||
private static RacingHistories initializeRacingHistories(RacingFleet racingFleet) { | ||||||
return RacingHistories.newInstance(racingFleet); | ||||||
} | ||||||
|
||||||
public void proceedWraps(int tryNumber) { | ||||||
|
@@ -43,19 +37,12 @@ public void proceedWraps(int tryNumber) { | |||||
} | ||||||
|
||||||
private void proceedWrap() { | ||||||
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. 자동차들이 1회 이동을 시도한 결과를 반환하면 이력을 분리할 수 있을 것 같아요.
Suggested change
|
||||||
int currentWrapNo = findCurrentWrapNo(); | ||||||
List<RacingCar> racingCars = this.racingFleet.getRacingCars(); | ||||||
racingCars.forEach(racingCar -> racingCar.race(RandomMovableStrategy.getInstance())); | ||||||
RacingCarStatesDTO carStates = RacingCarStatesDTO.valueOf(racingCars); | ||||||
this.wrapResults.getWrapResults().add(RacingWrapResultDTO.valueOf(currentWrapNo + 1, carStates)); | ||||||
racingFleet.raceAll(movableStrategy); | ||||||
this.histories.recordRacingState(racingFleet); | ||||||
} | ||||||
|
||||||
private int findCurrentWrapNo() { | ||||||
return Collections.max(this.wrapResults.getWrapResults(), Comparator.comparingInt(RacingWrapResultDTO::getWrapNumber)).getWrapNumber(); | ||||||
} | ||||||
|
||||||
public RacingWrapResultsDTO getWrapResults() { | ||||||
return this.wrapResults; | ||||||
public RacingHistories getHistories() { | ||||||
return this.histories; | ||||||
} | ||||||
|
||||||
public List<RacingCar> getRacingCars() { | ||||||
|
@@ -71,11 +58,11 @@ public boolean equals(Object o) { | |||||
if (this == o) return true; | ||||||
if (o == null || getClass() != o.getClass()) return false; | ||||||
CarRacing carRacing = (CarRacing) o; | ||||||
return Objects.equals(racingFleet, carRacing.racingFleet) && Objects.equals(wrapResults, carRacing.wrapResults); | ||||||
return Objects.equals(racingFleet, carRacing.racingFleet) && Objects.equals(histories, carRacing.histories); | ||||||
} | ||||||
|
||||||
@Override | ||||||
public int hashCode() { | ||||||
return Objects.hash(racingFleet, wrapResults); | ||||||
return Objects.hash(racingFleet, histories); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
package racingcar.domain; | ||
|
||
import racingcar.domain.movableStrategy.MovableStrategy; | ||
import racingcar.dto.RacingResultDTO; | ||
|
||
import java.util.List; | ||
|
||
public class RacingCarSimulator { | ||
public static RacingResultDTO simulate(List<String> carNames, int tryNumber) { | ||
CarRacing carRacing = CarRacing.valueOf(carNames); | ||
public static RacingResultDTO simulate(List<String> carNames, int tryNumber, MovableStrategy movableStrategy) { | ||
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. RacingCarSimulator 클래스는 상태도 없고 simulate 메서드의 로직은 컨트롤러의 로직이라고 생각되는데요. |
||
CarRacing carRacing = CarRacing.valueOf(carNames, movableStrategy); | ||
proceedRacing(tryNumber, carRacing); | ||
return RacingResultDTO.valueOf(carRacing); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package racingcar.domain; | ||
|
||
import java.util.Objects; | ||
|
||
public class RacingCarState { | ||
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. CarRacing 내부에 RacingHistories 필드를 가지다 보니 많은 클래스가 추가되고 전체적인 복잡도가 상승한 것 같아요. |
||
private final String carName; | ||
private final int carNo; | ||
private final int position; | ||
|
||
private RacingCarState(String carName, int carNo, int position) { | ||
this.carName = carName; | ||
this.carNo = carNo; | ||
this.position = position; | ||
} | ||
|
||
public static RacingCarState valueOf(RacingCar racingCar) { | ||
return new RacingCarState(racingCar.getName(), racingCar.getCarNo(), racingCar.getPosition()); | ||
} | ||
|
||
public String getCarName() { | ||
return this.carName; | ||
} | ||
|
||
public int getCarNo() { | ||
return this.carNo; | ||
} | ||
|
||
public int getPosition() { | ||
return this.position; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
RacingCarState that = (RacingCarState) o; | ||
return getCarNo() == that.getCarNo() && getPosition() == that.getPosition() && Objects.equals(getCarName(), that.getCarName()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(getCarName(), getCarNo(), getPosition()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package racingcar.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class RacingCarStates { | ||
private final List<RacingCarState> carStates; | ||
|
||
private RacingCarStates(List<RacingCarState> carStates) { | ||
this.carStates = carStates; | ||
} | ||
|
||
public static RacingCarStates valueOf(RacingFleet fleet) { | ||
List<RacingCar> racingCars = fleet.getRacingCars(); | ||
List<RacingCarState> carStates = new ArrayList<>(); | ||
for (RacingCar racingCar : racingCars) { | ||
carStates.add(RacingCarState.valueOf(racingCar)); | ||
} | ||
return new RacingCarStates(carStates); | ||
} | ||
|
||
public List<RacingCarState> value() { | ||
return this.carStates; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
RacingCarStates that = (RacingCarStates) o; | ||
return Objects.equals(value(), that.value()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(value()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package racingcar.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class RacingHistories { | ||
private final List<RacingHistory> value; | ||
|
||
private RacingHistories(List<RacingHistory> histories) { | ||
this.value = histories; | ||
} | ||
|
||
public static RacingHistories newInstance(RacingFleet racingFleet) { | ||
List<RacingHistory> histories = new ArrayList<>(); | ||
histories.add(RacingHistory.valueOf(0, RacingCarStates.valueOf(racingFleet))); | ||
return new RacingHistories(histories); | ||
} | ||
|
||
public List<RacingHistory> value() { | ||
return value; | ||
} | ||
|
||
public void recordRacingState(RacingFleet racingFleet) { | ||
RacingCarStates carStates = RacingCarStates.valueOf(racingFleet); | ||
value.add(RacingHistory.valueOf(findCurrentWrapNo() + 1, carStates)); | ||
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. 기존에 |
||
} | ||
|
||
private int findCurrentWrapNo() { | ||
return Collections.max(value, Comparator.comparingInt(RacingHistory::getWrapNumber)).getWrapNumber(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
RacingHistories that = (RacingHistories) o; | ||
return Objects.equals(value, that.value); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(value); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package racingcar.domain; | ||
|
||
import java.util.Objects; | ||
|
||
public class RacingHistory { | ||
private final int wrapNumber; | ||
private final RacingCarStates carStates; | ||
|
||
private RacingHistory(int wrapNumber, RacingCarStates catStates) { | ||
this.wrapNumber = wrapNumber; | ||
this.carStates = catStates; | ||
} | ||
|
||
public static RacingHistory valueOf(int wrapNumber, RacingCarStates catStates) { | ||
return new RacingHistory(wrapNumber, catStates); | ||
} | ||
|
||
public int getWrapNumber() { | ||
return this.wrapNumber; | ||
} | ||
|
||
public RacingCarStates getCarStates() { | ||
return this.carStates; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
RacingHistory that = (RacingHistory) o; | ||
return getWrapNumber() == that.getWrapNumber() && Objects.equals(getCarStates(), that.getCarStates()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(getWrapNumber(), getCarStates()); | ||
} | ||
} |
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.
이동 전략을 Main에서 주입하도록 개선해주셨네요 👍