Search
Duplicate
🚀

4단계 - 자동차 경주(우승자)

1. 공백 라인을 의미있게 사용해보자.

기능/문맥 단위로 공백을 주어 코드의 가독성을 높히도록 하자
//before case RacingGameImpl game = new RacingGameImpl(); InputView inputView = InputView.getInstance(); ResultView resultView = ResultView.getInstance(); RacingInfomation racingInfomation = inputView.execute(new CarNameInputStrategy()); game.execute(racingInfomation, MoveStrategy.defaultStrategy(new Random())); resultView.viewAll(game.getGameHistory(), new PrintMarkStrategy("-")); //after case RacingGameImpl game = new RacingGameImpl(); InputView inputView = InputView.getInstance(); ResultView resultView = ResultView.getInstance(); RacingInfomation racingInfomation = inputView.execute(new CarNameInputStrategy()); game.execute(racingInfomation, MoveStrategy.defaultStrategy(new Random())); resultView.viewAll(game.getGameHistory(), new PrintMarkStrategy("-"));
Java
복사

2. 요구사항으로 인한 객체 수정 팁

요구사항으로 인해 객체의 상태값이 변경되는 일은 빈번하다.
그렇다고 매 번 추가되는 상태값에 따라 새로운 하위클래스를 만들필요는 없다.
추가되는 상태값이 전혀 연관없는 값일 경우에만 클래스를 분리하며 그렇지 않고 객체명에 연관되어 객체의 의미가 훼손되지않는 경우에는 객체를 수정하는편이 좋다.

3. 전략의 주입시점은 전략을 사용하는 시점에서

처음 생성자의 생성시점이나 setter를 통해 전략을 주입하는 것 보다는 해당 전략이 필요한 시점에 전략을 주입하면 조금 더 유연하게 대처할 수 있다.

4. compareTo와 equals의 결과는 같아야 한다.

compareTo가 "같다" 라고 판단했다면, equals가 true여야 한다.
:Java Comparable.compareTo(T o)문서에서 (x.compareTo(y)==0) == (x.equals(y)) 구현하도록 강하게 권장하고 있다.(필수는 아니지만 가능한 준수하라)
주제의 역순인 eqaul가 true라면 compareTo는 0이여야 한다 라는 명제는 성립할 필요가 없다.

5. Collections.unmodifiablelist()를 활용해 read-olny개념으로 변조를 막자.

//before case return racingCars.stream() .filter(rc -> rc.isSameMoveCount(winner)) .collect(Collectors.toList()); //after case racingCars.stream() .filter(rc -> rc.isSameMoveCount(winner)) .collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
Java
복사

6. Stream은 for-loop보다 퍼포먼스가 떨어지는데 어째서 사용되는가?

Stream과 for-loop과 성능을 비교하면 Stream이 떨어지지만, 그게 유의미할정도의 큰 성능차이를 보기는 힘들다.
이정도의 퍼포먼스까지 신경써야 할 정도이면 C나 C++을 사용하는게 더 좋다.
Stream을 사용하는 가장 큰 목적은 사람 관점에서 더 이해하기 쉬운 반복을 만드는 것.