-
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
Step5 - 리펙토링 #6073
Step5 - 리펙토링 #6073
Changes from 5 commits
740c2b5
d037432
53bdd18
12a3f4c
6e99d3e
65da907
09d392f
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,48 @@ | ||
package race; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
public class CarList { | ||
private final Random random = new Random(); | ||
|
||
private final List<RacingCar> cars; | ||
|
||
public CarList() { | ||
this(new ArrayList<>()); | ||
} | ||
|
||
public CarList(List<RacingCar> cars) { | ||
this.cars = cars; | ||
} | ||
|
||
public void add(RacingCar car) { | ||
cars.add(car); | ||
} | ||
|
||
public void moveWithRandom() { | ||
for (RacingCar car : cars) { | ||
car.moveWithSeed(random.nextInt()); | ||
} | ||
} | ||
|
||
public List<RacingCar> getList() { | ||
return cars; | ||
} | ||
|
||
public List<RacingCar> getWinners() { | ||
Position maxPosition = new Position(); | ||
for (RacingCar car : cars) { | ||
maxPosition = car.getMaxPosition(maxPosition); | ||
} | ||
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. comparable 을 구현하여 아래와 같이 sort 를 적용해보았습니다. cars.sort(Collections.reverseOrder());
RacingCar winner = cars.get(0); |
||
|
||
List<RacingCar> winners = new ArrayList<>(); | ||
|
||
for (RacingCar car : cars) { | ||
if (car.isSamePosition(maxPosition)) winners.add(car); | ||
} | ||
return winners; | ||
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. 여기도 stream API 로 개선 시도해주세요. |
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package race; | ||
|
||
public class CarName { | ||
private final String name; | ||
|
||
public CarName(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return name; | ||
} | ||
|
||
} |
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,44 @@ | ||
package race; | ||
|
||
import java.util.Objects; | ||
|
||
public class Position { | ||
private int value; | ||
|
||
public Position() { | ||
this(0); | ||
} | ||
|
||
public Position(int value) { | ||
this.value = value; | ||
} | ||
|
||
public Position move() { | ||
this.value++; | ||
return this; | ||
} | ||
|
||
public int getValue() { | ||
return this.value; | ||
} | ||
|
||
public Position getMax(Position comparePosition) { | ||
if (this.value > comparePosition.getValue()) { | ||
return this; | ||
} | ||
return comparePosition; | ||
} | ||
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. 아래와 같이 compareTo 오버라이드 하였습니다 @Override
public int compareTo(Position o) {
return Integer.compare(this.value, o.value);
} |
||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) return true; | ||
if (obj == null || getClass() != obj.getClass()) return false; | ||
Position position = (Position) obj; | ||
return value == position.value; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(value); | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,48 @@ | ||
package race; | ||
|
||
public class RacingCar implements Comparable<RacingCar> { | ||
public class RacingCar { | ||
private static final int MAX_NAME_LENGTH = 5; | ||
private final String name; | ||
private int position; | ||
private final RandomNumberGenerator randomNumberGenerator; | ||
private final PositionPrinter positionPrinter; | ||
private static final int MOVE_CRITERIA = 4; | ||
private final CarName name; | ||
private final Position position; | ||
|
||
private RacingCar(String name, RandomNumberGenerator randomNumberGenerator, PositionPrinter positionPrinter) { | ||
this.name = name; | ||
this.randomNumberGenerator = randomNumberGenerator; | ||
this.positionPrinter = positionPrinter; | ||
public RacingCar(String name) { | ||
this(name, 0); | ||
} | ||
|
||
@Override | ||
public int compareTo(RacingCar other) { | ||
return Integer.compare(this.position, other.position); | ||
public RacingCar(String name, int position) { | ||
this.name = new CarName(name); | ||
this.position = new Position(position); | ||
} | ||
|
||
public static boolean validateName(String carName) { | ||
return !carName.isEmpty() && carName.length() <= MAX_NAME_LENGTH; | ||
} | ||
|
||
private boolean shouldMove(int num) { | ||
return num >= 4; | ||
return num >= MOVE_CRITERIA; | ||
} | ||
|
||
public int move() { | ||
int randomValue = randomNumberGenerator.generate(); | ||
if (shouldMove(randomValue)) { | ||
position++; | ||
public Position moveWithSeed(int seed) { | ||
if (shouldMove(seed)) { | ||
return this.position.move(); | ||
} | ||
return position; | ||
return this.position; | ||
} | ||
|
||
public void printPosition() { | ||
positionPrinter.printPosition(name, position); | ||
public CarName getName() { | ||
return this.name; | ||
} | ||
|
||
static RacingCar create(String racingName, RandomNumberGenerator randomNumberGenerator, PositionPrinter positionPrinter) { | ||
return new RacingCar(racingName, randomNumberGenerator, positionPrinter); | ||
public Position getPosition() { | ||
return this.position; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
public Position getMaxPosition(Position comparePosition) { | ||
return this.position.getMax(comparePosition); | ||
} | ||
|
||
public boolean isSamePosition(Position comparePosition) { | ||
return this.position.equals(comparePosition); | ||
} | ||
} |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package race; | ||
|
||
public class TrackCondition { | ||
private final int maxCarCount; | ||
private final int maxAttemptCount; | ||
|
||
public TrackCondition(int maxCarCount, int maxAttemptCount) { | ||
this.maxCarCount = maxCarCount; | ||
this.maxAttemptCount = maxAttemptCount; | ||
} | ||
|
||
public boolean validateCarCount(int num) { | ||
return num >= 1 && num <= this.maxCarCount; | ||
} | ||
|
||
public boolean validateAttemptCount(int num) { | ||
return num >= 1 && num <= this.maxAttemptCount; | ||
} | ||
} |
This file was deleted.
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.
이 클래스의 역할과 책임이 너무 비대한 것 같습니다. 우선
CarList
는 일급 콜렉션임을 염두에 두고 개발 하셨나요?그렇다면
random
변수는 조금 과하다는 생각이 듭니다. 아울러, 제가 지난 스텝에서도 한번 말씀드린 부분이기는 한데, 인스턴스 변수에 직접new
를 호출해 값을 할당함으로써 테스트 가능성이 극적으로 줄어든 것 같습니다.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.
다음과 같이 moveWithRandom 에서 random을 주입 받도록 수정했습니다.