-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat : step3 #4243
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
base: kay019
Are you sure you want to change the base?
feat : step3 #4243
Conversation
javajigi
left a comment
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.
2단계 객체 설계가 좋아 3단계에 큰 변화 없이 요구사항 구현했네요. 👍
단, 아직까지 절차 지향적인 사고로 접근하는 코드가 보여 피드백 남깁니다.
2단계에도 남겼던 피드백인데요.
한번 반영해 보고 테스트 코드에도 달라지는 부분이 있을지 고민해 보는 시간을 가지면 좋겠습니다.
| @@ -0,0 +1,24 @@ | |||
| package lotto; | |||
|
|
|||
| public class BonusNumber { | |||
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.
👍
단, 이와 같이 포장한 객체의 특성은 Lotto의 Set에 Integer도 같다.
Set 콜렉션의 Integer 값도 이 객체를 사용하도록 리팩터링해보면 어떨까?
| public MatchResult match(Lotto ticket) { | ||
| int matchCount = ticket.matchCount(winning); | ||
| boolean bonusMatched = ticket.contains(bonus.value()); | ||
| return new MatchResult(matchCount, bonusMatched); | ||
| } |
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.
| public MatchResult match(Lotto ticket) { | |
| int matchCount = ticket.matchCount(winning); | |
| boolean bonusMatched = ticket.contains(bonus.value()); | |
| return new MatchResult(matchCount, bonusMatched); | |
| } | |
| public Rank match(Lotto ticket) { | |
| ... | |
| } |
이 메서드에서 등수를 결정해 Rank를 반환하는 것은 어떨까?
| private final Lotto winning; | ||
| private final BonusNumber bonus; | ||
|
|
||
| public WinningNumbers(Lotto winning, BonusNumber bonus) { |
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.
| public WinningNumbers(Lotto winning, BonusNumber bonus) { | |
| public WinningNumbers(Lotto winning, int bonus) { |
포장한 객체가 아닌 부 생성자도 지원하면 이 객체를 생성할 때 얻을 수 있는 이점이 있을까?
| WinningStatistics stats = new WinningStatistics(); | ||
| stats.accumulate(tickets, winningNumbers); |
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.
| WinningStatistics stats = new WinningStatistics(); | |
| stats.accumulate(tickets, winningNumbers); | |
| WinningStatistics stats = winning.match(lottos); |
위와 같이 구현하는 것은 어떨까?
위와 같이 구현할 경우 WinningStatistics에 대한 단위 테스트 코드가 어떻게 달라지는지를 현재 코드와 비교해 본다.
javajigi
left a comment
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.
피드백 반영하느라 수고했어요. 👍
단, 피드백 반영한 내용 중 아쉬운 부분이 있어 피드백 남겼어요.
특히 public void accumulate(List<Lotto> lottos, Lotto winning) {와 같이 구현하기보다 Lotto 객체 또는 lottos를 포장해 일급 콜렉션을 Lottos로 구현한다면 이 같은 객체에 메시지를 보내도록 접근하기 위해 의식적인 연습을 해보면 좋겠습니다.
이렇게 구현했을 때의 이점에 대해서도 고민해 보는 시간을 가져볼 것을 추천해요.
| @@ -0,0 +1,24 @@ | |||
| package lotto; | |||
|
|
|||
| public class BonusNumber { | |||
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.
LottoNumber와 이 객체의 속성이 같다.
LottoNumber을 사용하고 이 객체를 제거하는 것은 어떨까?
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class LottoNumber { |
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.
상황
- 1만명의 사용자가 동시에 당첨 여부를 확인할 수 있어야 한다.
- 1명의 사용자는 평균 5장의 로또를 구매한 상태이다.
위 요구사항을 서버에서 생성되는 LottoNumber의 인스턴스의 갯수는?
1 * 5장 * 6개 숫자/1장 * 1만명 = 30만개이다.
동시에 생성되는 인스턴스 갯수가 너무 많다.
인스턴스 갯수를 줄일 수 있는 방법은?
로또 숫자 값을 LottoNumber 객체로 래핑하는 경우 매번 인스턴스가 생성되기 때문에 인스턴스의 갯수가 너무 많아져 성능이 떨어질 수 있다.
LottoNumber 인스턴스를 생성한 후 재사용할 수 있도록 구현한다.
힌트 : Map과 같은 곳에 인스턴스를 생성한 후 재사용하는 방법을 찾아본다.
| public WinningStatistics match(List<Lotto> tickets) { | ||
| WinningStatistics stats = new WinningStatistics(); | ||
| stats.accumulate(tickets, this); | ||
| return stats; | ||
| } |
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.
| public WinningStatistics match(List<Lotto> tickets) { | |
| WinningStatistics stats = new WinningStatistics(); | |
| stats.accumulate(tickets, this); | |
| return stats; | |
| } | |
| public WinningStatistics match(List<Lotto> tickets) { | |
| WinningStatistics stats = new WinningStatistics(); | |
| for(Lotto ticket: tickets) { | |
| stats.accumlate(match(ticket)); | |
| } | |
| return stats; | |
| } |
위와 같이 구현하는 것은 어떨까?
이렇게 구현할 경우 WinningStatistics의 테스트 코드 구현이 어떻게 달라질까?
현재 구현과 위와 같이 구현했을 때의 어떤 접근 방법이 더 좋을지 생각해 보는 시간을 가지면 좋겠다.
| private static final int MIN = 1; | ||
| private static final int MAX = 45; | ||
|
|
||
| private final Set<Integer> numbers; |
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.
| private final Set<Integer> numbers; | |
| private final Set<LottoNumber> numbers; |
콜렉션의 값이 원시 값이라면 원시 값을 포장해 구현해 본다.
이렇게 구현했을 때 얻을 수 있는 이점은 무엇인가?
step 3 리뷰 반영했습니다