-
Notifications
You must be signed in to change notification settings - Fork 62
[사다리 - 함수형 프로그래밍] 박진형 리팩터링 #87
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
Open
jinhyeongpark
wants to merge
47
commits into
next-step:jinhyeongpark
Choose a base branch
from
jinhyeongpark:step2
base: jinhyeongpark
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
c907d1c
feat: 사다리의 가로, 세로 값에 대한 원시값 포장
jinhyeongpark 3df5893
feat: 사다리의 지점 상태를 관리하는 Point 도메인 구현
jinhyeongpark 8d03131
feat: 사다리의 가로 한 줄을 담당하는 Line 도메인 구현
jinhyeongpark 9b6cb0c
feat: 사다리의 전체 구조를 관리하는 Ladder 도메인 구현
jinhyeongpark 0342e8f
feat: 사다리의 시각화를 위한 OutputView 구현\
jinhyeongpark 301cd44
feat: 사용자 입력을 담당하는 InputView 구현
jinhyeongpark 5ca9ff3
feat: 프로그램 실행을 담당하는 Application 메인 로직 구현
jinhyeongpark c5552c9
feat: Line의 유무를 판단하는 Point의 boolean에 대해 BooleanSupplier 적용
jinhyeongpark ae958f0
feat: 사용하지 않는 기존 canDraw 메서드 삭제
jinhyeongpark a6ea645
test: 값 객체(Point, Width, Height)에 대한 단위 테스트 추가
jinhyeongpark 605275b
test: Line의 다리 생성 및 가로 중복 금지 로직 검증 테스트 추가
jinhyeongpark a9ff74b
test: 사다리 전체 구조 및 조립 로직 검증 테스트 추가
jinhyeongpark a5ed58a
feat: 하나의 Line 내에서의 이동 로직 메서드 move 구현
jinhyeongpark 2071d3e
feat: Ladder에서 사다리 게임 수행 및 결과를 생성하는 메서드 구현
jinhyeongpark 28fe4d9
feat: OutputView에서 사다리게임 결과 출력 메서드 구현
jinhyeongpark d101e00
test: move에 대한 테스트코드 작성
jinhyeongpark 614c62d
test: climb과 generateResults에 대한 테스트코드 작성
jinhyeongpark 49d73b3
refactor: LadderTest에서 반복되는 Participants 생성에 대해 상수 및 메서드로 분리
jinhyeongpark b3ac3e1
feat: Ladder의 정적 팩터리 메서드 파라미터 변경
jinhyeongpark 349cf9f
feat: 사용하지 않는 LadderWidth 관련 클래스 삭제
jinhyeongpark ff2eb5e
feat: 실행 결과와 결과를 담는 일급 컬렉션 구현
jinhyeongpark 10df688
feat: 참여자 명과 참여자들을 관리하는 일급 컬렉션 구현
jinhyeongpark 076b917
feat: 참여자와 실행 결과에 대한 입력 로직 구현
jinhyeongpark b21eb78
feat: 단일 필드를 반환하는 메서드를 추가하여 toString() 없이 문자열로 출력되도록 함
jinhyeongpark 11048f6
feat: 단일 필드를 반환하는 메서드를 추가하여 toString() 없이 문자열로 출력되도록 함
jinhyeongpark bbd4b73
feat: 사다리 게임 결과 매핑을 위한 LadderGameResult 클래스 추가
jinhyeongpark 591482a
feat: 게임 결과 조회 흐름을 제어하는 LadderGameController 추가
jinhyeongpark 3c81f19
feat: 특정 참여자의 결과를 요청하는 입력 메서드 구현
jinhyeongpark 8dbf07e
feat: 사다리 시각화 정렬 및 게임 결과 출력 기능 구현
jinhyeongpark 33f1f4f
feat: 사다리 게임 참가자 및 결과 입력 처리 로직 추가
jinhyeongpark d5090da
test: Name 클래스 단위 테스트 구현
jinhyeongpark 6f66089
test: LadderResult 및 LadderResults 클래스 단위 테스트 구현
jinhyeongpark eb01867
test: LadderGameResultTest 클래스 단위 테스트 구현
jinhyeongpark 19f0bd5
test: ParticipantsTest 구현
jinhyeongpark 32e7fda
refactor: LadderResults의 results 반환 메서드에 대해 수정 방지를 위한 불변값으로 변경
jinhyeongpark dc96a90
refactor: Ladder 클래스 책임 분리 및 Lines 일급 컬렉션 도입
jinhyeongpark c23c944
test: Lines 단위 테스트 추가 및 LadderTest 리팩터링
jinhyeongpark 0b9b1e5
refactor: 사용하지 않는 메서드 삭제
jinhyeongpark 0b57f6f
refactor: climb 메서드 접근제어자 변경
jinhyeongpark 0827136
refactor: Point 클래스에 대해 Enum으로 변경
jinhyeongpark d3b0264
refactor: Participants의 from 메서드 수정
jinhyeongpark 5b08244
refactor: Line의 Point 리스트를 Points 일급 컬렉션으로 리팩터링
jinhyeongpark cb14f4b
refactor: Line의 Point 리스트를 Points 일급 컬렉션으로 리팩터링
jinhyeongpark b0e4e34
test: Points 단위 테스트 추가
jinhyeongpark 8fda279
refactor: 완성된 Lines 주입 방식으로 Ladder 생성 로직 리팩터링 및 DI 적용
jinhyeongpark 7f4a112
test: 리팩터링된 도메인 구조에 맞춰 Ladder 및 Lines 테스트 수정
jinhyeongpark 402eb27
Merge branch 'jinhyeongpark' into step2
jinhyeongpark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,54 +1,40 @@ | ||
| package ladder.domain; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.BooleanSupplier; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| public class Ladder { | ||
|
|
||
| private final List<Line> lines; | ||
| private final Lines lines; | ||
| private final int width; | ||
|
|
||
| private Ladder(List<Line> lines, int width) { | ||
| private Ladder(Lines lines, int width) { | ||
| this.lines = lines; | ||
| this.width = width; | ||
| } | ||
|
|
||
| public static Ladder of(Participants participants, LadderHeight height, BooleanSupplier strategy) { | ||
| int participantCount = participants.size(); | ||
| List<Line> lines = generateLines(participantCount, height, strategy); | ||
|
|
||
| return new Ladder(lines, participantCount); | ||
| public static Ladder of(Participants participants, Lines lines) { | ||
| return new Ladder(lines, participants.size()); | ||
| } | ||
|
|
||
| public int climb(int startIndex) { | ||
| int currentIndex = startIndex; | ||
| for (Line line : lines) { | ||
| currentIndex = line.move(currentIndex); | ||
| } | ||
| return currentIndex; | ||
| int climb(int startIndex) { | ||
| return lines.move(startIndex); | ||
| } | ||
|
|
||
| public Map<Integer, Integer> generateResults() { | ||
| Map<Integer, Integer> results = new LinkedHashMap<>(); | ||
| for (int i = 0; i < width; i++) { | ||
| results.put(i, climb(i)); | ||
| } | ||
| return results; | ||
| } | ||
|
|
||
| private static List<Line> generateLines(int participantCount, LadderHeight height, BooleanSupplier strategy) { | ||
| List<Line> lines = new ArrayList<>(); | ||
| int pointCount = participantCount - 1; | ||
| for (int i = 0; i < height.getValue(); i++) { | ||
| lines.add(Line.from(pointCount, strategy)); | ||
| } | ||
| return lines; | ||
| return IntStream.range(0, width) | ||
| .boxed() | ||
| .collect(Collectors.toMap( | ||
| index -> index, | ||
| this::climb, | ||
| (oldValue, newValue) -> newValue, | ||
| LinkedHashMap::new | ||
| )); | ||
|
Comment on lines
+27
to
+34
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. 이건 반복문 쓰는게 더 코드가 직관적일 것 같은데요?? 스트림을 선택한 이유가 있었나요?? |
||
| } | ||
|
|
||
| public List<Line> getLines() { | ||
| public Lines getLines() { | ||
| return lines; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,47 +1,24 @@ | ||
| package ladder.domain; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.function.BooleanSupplier; | ||
|
|
||
| public class Line { | ||
|
|
||
| private final List<Point> points; | ||
| private final Points points; | ||
|
|
||
| private Line(List<Point> points) { | ||
| private Line(Points points) { | ||
| this.points = points; | ||
| } | ||
|
|
||
| public static Line from(int size, BooleanSupplier strategy) { | ||
| return new Line(generatePoints(size, strategy)); | ||
| } | ||
|
|
||
| public List<Point> getPoints() { | ||
| return points; | ||
| public static Line generate(int size, BooleanSupplier strategy) { | ||
| return new Line(Points.generate(size, strategy)); | ||
| } | ||
|
|
||
| public int move(int index) { | ||
| if (index < points.size() && points.get(index).hasBridge()) { | ||
| return index + 1; | ||
| } | ||
| if (index > 0 && points.get(index - 1).hasBridge()) { | ||
| return index - 1; | ||
| } | ||
| return index; | ||
| return points.move(index); | ||
| } | ||
|
|
||
| private static List<Point> generatePoints(int size, BooleanSupplier strategy) { | ||
| List<Point> points = new ArrayList<>(); | ||
| boolean lastBridge = false; | ||
| for (int i = 0; i < size; i++) { | ||
| lastBridge = addPoint(points, lastBridge, strategy); | ||
| } | ||
| public Points getPoints() { | ||
| return points; | ||
| } | ||
|
|
||
| private static boolean addPoint(List<Point> points, boolean lastBridge, BooleanSupplier strategy) { | ||
| boolean currentBridge = !lastBridge && strategy.getAsBoolean(); | ||
| points.add(Point.from(currentBridge)); | ||
| return currentBridge; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package ladder.domain; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.function.BooleanSupplier; | ||
|
|
||
| public class Lines implements Iterable<Line> { | ||
| private final List<Line> lines; | ||
|
|
||
| private Lines(List<Line> lines) { | ||
| this.lines = lines; | ||
| } | ||
|
|
||
| public static Lines generate(int participantCount, LadderHeight height, BooleanSupplier strategy) { | ||
| List<Line> lines = new ArrayList<>(); | ||
| int pointCount = participantCount - 1; | ||
| for (int i = 0; i < height.getValue(); i++) { | ||
| lines.add(Line.generate(pointCount, strategy)); | ||
| } | ||
| return new Lines(lines); | ||
| } | ||
|
|
||
| public int move(int startIndex) { | ||
| int currentIndex = startIndex; | ||
| for (Line line : lines) { | ||
| currentIndex = line.move(currentIndex); | ||
| } | ||
| return currentIndex; | ||
| } | ||
|
|
||
| public Line get(int index) { | ||
| return lines.get(index); | ||
| } | ||
|
|
||
| public int size() { | ||
| return lines.size(); | ||
| } | ||
|
|
||
| @Override | ||
| public Iterator<Line> iterator() { | ||
| return lines.iterator(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,18 @@ | ||
| package ladder.domain; | ||
|
|
||
| public class Point { | ||
|
|
||
| private static final Point BRIDGE = new Point(true); | ||
| private static final Point EMPTY = new Point(false); | ||
| public enum Point { | ||
| BRIDGE(true), | ||
| EMPTY(false); | ||
|
|
||
| private final boolean hasBridge; | ||
|
|
||
| private Point(boolean hasBridge) { | ||
| Point(boolean hasBridge) { | ||
| this.hasBridge = hasBridge; | ||
| } | ||
|
|
||
| public static Point from(boolean hasBridge) { | ||
| if (hasBridge) { | ||
| return BRIDGE; | ||
| } | ||
| return EMPTY; | ||
| return hasBridge ? BRIDGE : EMPTY; | ||
| } | ||
|
|
||
| public boolean hasBridge() { | ||
| return hasBridge; | ||
| } | ||
| public boolean hasBridge() { return hasBridge; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package ladder.domain; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.function.BooleanSupplier; | ||
|
|
||
| public class Points implements Iterable<Point> { | ||
| private final List<Point> points; | ||
|
|
||
| private Points(List<Point> points) { | ||
| this.points = points; | ||
| } | ||
|
|
||
| public static Points generate(int size, BooleanSupplier strategy) { | ||
| List<Point> points = new ArrayList<>(); | ||
| boolean lastBridge = false; | ||
| for (int i = 0; i < size; i++) { | ||
| lastBridge = addPoint(points, lastBridge, strategy); | ||
| } | ||
| return new Points(points); | ||
| } | ||
|
|
||
| private static boolean addPoint(List<Point> points, boolean lastBridge, BooleanSupplier strategy) { | ||
| boolean currentBridge = !lastBridge && strategy.getAsBoolean(); | ||
| points.add(Point.from(currentBridge)); | ||
| return currentBridge; | ||
| } | ||
|
|
||
| public int move(int index) { | ||
| if (isRightStep(index)) { | ||
| return index + 1; | ||
| } | ||
| if (isLeftStep(index)) { | ||
| return index - 1; | ||
| } | ||
| return index; | ||
| } | ||
|
|
||
| private boolean isRightStep(int index) { | ||
| return index < points.size() && points.get(index).hasBridge(); | ||
| } | ||
|
|
||
| private boolean isLeftStep(int index) { | ||
| return index > 0 && points.get(index - 1).hasBridge(); | ||
| } | ||
|
|
||
| public Point get(int index) { | ||
| return points.get(index); | ||
| } | ||
|
|
||
| public int size() { | ||
| return points.size(); | ||
| } | ||
|
|
||
| public List<Point> getValues() { | ||
| return Collections.unmodifiableList(points); | ||
| } | ||
|
Comment on lines
+57
to
+59
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. Collections.unmodifiableList를 사용하면 모든 경우에서 불변을 유지할 수 있는걸까요?? |
||
|
|
||
| @Override | ||
| public Iterator<Point> iterator() { | ||
| return points.iterator(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
이 친구는 default로 설정한 이유가 있나요?