Skip to content

Commit 158b0ee

Browse files
committed
refactor : 공정성 향상 및 요구사항 기반 리팩토링
1 parent f423106 commit 158b0ee

File tree

8 files changed

+198
-37
lines changed

8 files changed

+198
-37
lines changed

Diff for: src/main/java/LadderApplication.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class LadderApplication {
44
public static void main(String[] args) {
5-
LadderController ladderController = new LadderController();
5+
LadderController ladderController = LadderController.getInstance();
66

77
ladderController.run();
88
}

Diff for: src/main/java/controller/LadderController.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
package controller;
22

3+
import dto.LineDto;
34
import model.Ladder;
45
import model.Line;
56
import view.LadderOutputView;
67

78
import java.util.List;
89

910
public class LadderController {
10-
LadderOutputView ladderOutputView = new LadderOutputView();
11+
private static final LadderController ladderController = new LadderController();
12+
13+
private final LadderOutputView ladderOutputView = LadderOutputView.getInstance();
14+
15+
private LadderController() {
16+
}
17+
18+
public static LadderController getInstance() {
19+
return ladderController;
20+
}
1121

1222
public void run() {
1323
Ladder ladder = new Ladder(4, 4);
1424
List<Line> lines = ladder.getLines();
1525

16-
for(Line line : lines) {
17-
ladderOutputView.printLine(line.getLinks());
26+
ladderOutputView.printResultHeader();
27+
for (Line line : lines) {
28+
LineDto lineDto = LineDto.from(line);
29+
ladderOutputView.printLine(lineDto);
1830
}
1931
}
2032

Diff for: src/main/java/dto/LineDto.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package dto;
2+
3+
import model.Line;
4+
import model.Link;
5+
import model.LinkStatus;
6+
7+
import java.util.List;
8+
9+
public class LineDto {
10+
private final List<Boolean> linkExistCollection;
11+
12+
private LineDto(List<Boolean> linkExistCollection) {
13+
this.linkExistCollection = linkExistCollection;
14+
}
15+
16+
public static LineDto from(Line line) {
17+
List<Boolean> linkExistCollection = line.getLinks().stream()
18+
.map(Link::getLinkstatus)
19+
.map(LinkStatus::isPresent)
20+
.toList();
21+
22+
return new LineDto(linkExistCollection);
23+
}
24+
25+
public List<Boolean> getLinkExistCollection() {
26+
return List.copyOf(linkExistCollection);
27+
}
28+
29+
}

Diff for: src/main/java/model/Ladder.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
import java.util.List;
66

77
public class Ladder {
8-
private List<Line> lines;
8+
private final List<Line> lines;
99

1010
public Ladder(int width, int height) {
1111
List<Line> lines = new ArrayList<>();
12-
for(int i = 0; i < height; i++) {
12+
for (int i = 0; i < height; i++) {
1313
lines.add(new Line(width));
1414
}
1515

@@ -19,4 +19,5 @@ public Ladder(int width, int height) {
1919
public List<Line> getLines() {
2020
return List.copyOf(lines);
2121
}
22+
2223
}

Diff for: src/main/java/model/Line.java

+72-18
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,99 @@
11
package model;
22

3-
import java.sql.Array;
43
import java.util.ArrayList;
5-
import java.util.Collections;
64
import java.util.List;
75
import java.util.Random;
86

7+
import static model.LinkStatus.PRESENT;
8+
import static model.LinkStatus.UNDEFINED;
9+
910
public class Line {
1011
private static final Random random = new Random();
1112

12-
private final List<Boolean> links;
13+
private final List<Link> links;
1314

1415
public Line(int width) {
15-
List<Boolean> links = new ArrayList<>();
16-
for (int i = 0; i < width-1; i++) {
17-
links.add(getConnectDecider(i, links));
16+
int size = width - 1;
17+
List<Link> links = initializeLinks(size);
18+
19+
setupLinks(links);
20+
21+
this.links = List.copyOf(links);
22+
}
23+
24+
public List<Link> getLinks() {
25+
return List.copyOf(links);
26+
}
27+
28+
private List<Link> initializeLinks(int size) {
29+
List<Link> links = new ArrayList<>();
30+
for (int i = 0; i < size; i++) {
31+
links.add(Link.getUndefinedLink());
1832
}
1933

20-
this.links = Collections.unmodifiableList(links);
34+
return List.copyOf(links);
2135
}
2236

23-
// 메서드명 미정
24-
private boolean getConnectDecider (int index, List<Boolean> links) {
25-
if(!isConnectable(index, links)) {
26-
return false;
37+
private void setupLinks(List<Link> links) {
38+
int index = getRandomStartIndex(links);
39+
40+
while (containsUndefined(links)) {
41+
boolean connectDecider = getConnectDecider();
42+
boolean connectable = isConnectable(index, links);
43+
44+
Link link = links.get(index);
45+
link.setLinkStatus(connectDecider, connectable);
46+
47+
index = getNextIndex(index, links);
2748
}
28-
// 랜덤 돌리고
49+
}
50+
51+
private int getRandomStartIndex(List<Link> links) {
52+
return random.nextInt(links.size());
53+
}
54+
55+
private boolean containsUndefined(List<Link> links) {
56+
return links.stream()
57+
.anyMatch(link -> link.getLinkstatus() == UNDEFINED);
58+
}
59+
60+
private boolean getConnectDecider() {
2961
return random.nextBoolean();
3062
}
3163

32-
// 연결 가능한지 검사
33-
private boolean isConnectable(int index, List<Boolean> links) {
34-
return isFirstIndex(index) || !(links.get(index-1));
64+
private boolean isConnectable(int index, List<Link> links) {
65+
if (isFirstIndex(index)) {
66+
return isRightNotPresent(index, links);
67+
}
68+
if (isLastIndex(index, links)) {
69+
return isLeftNotPresent(index, links);
70+
}
71+
72+
return isRightNotPresent(index, links) && isLeftNotPresent(index, links);
3573
}
3674

37-
// 첫번째 인덱스인지 확인
3875
private boolean isFirstIndex(int index) {
3976
return index == 0;
4077
}
4178

42-
public List<Boolean> getLinks() {
43-
return List.copyOf(links);
79+
private boolean isLastIndex(int index, List<Link> links) {
80+
return index == links.size() - 1;
4481
}
82+
83+
private boolean isRightNotPresent(int index, List<Link> links) {
84+
Link rightLink = links.get(index + 1);
85+
86+
return rightLink.getLinkstatus() != PRESENT;
87+
}
88+
89+
private boolean isLeftNotPresent(int index, List<Link> links) {
90+
Link leftLink = links.get(index - 1);
91+
92+
return leftLink.getLinkstatus() != PRESENT;
93+
}
94+
95+
private int getNextIndex(int index, List<Link> linkStatuses) {
96+
return ++index % linkStatuses.size();
97+
}
98+
4599
}

Diff for: src/main/java/model/Link.java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package model;
2+
3+
public class Link {
4+
private LinkStatus linkstatus;
5+
6+
private Link(LinkStatus linkstatus) {
7+
this.linkstatus = linkstatus;
8+
}
9+
10+
public static Link getUndefinedLink() {
11+
return new Link(LinkStatus.UNDEFINED);
12+
}
13+
14+
public void setLinkStatus(boolean connectDecider, boolean connectable) {
15+
validateUpdateStatus();
16+
17+
if (connectDecider && connectable) {
18+
this.linkstatus = LinkStatus.PRESENT;
19+
return;
20+
}
21+
22+
this.linkstatus = LinkStatus.ABSENT;
23+
}
24+
25+
public LinkStatus getLinkstatus() {
26+
return linkstatus;
27+
}
28+
29+
private void validateUpdateStatus() {
30+
if (isAlreadyFixed()) {
31+
throw new UnsupportedOperationException("이미 값이 결정된 링크입니다");
32+
}
33+
}
34+
35+
private boolean isAlreadyFixed() {
36+
return this.linkstatus != LinkStatus.UNDEFINED;
37+
}
38+
39+
}

Diff for: src/main/java/model/LinkStatus.java

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package model;
2+
3+
public enum LinkStatus {
4+
UNDEFINED, ABSENT, PRESENT;
5+
6+
public boolean isPresent() {
7+
return this == present;
8+
}
9+
10+
}

Diff for: src/main/java/view/LadderOutputView.java

+29-13
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,47 @@
11
package view;
22

3-
import java.util.List;
3+
import dto.LineDto;
44

55
public class LadderOutputView {
66

7-
private static String DASH_COUPLER = "-----";
8-
private static String BLANK_COUPLER = " ";
7+
private static final String INDENTATION = " ";
8+
private static final String DASH_COUPLER = "-----";
9+
private static final String BLANK_COUPLER = " ";
10+
private static final String PILLAR = "|";
911

10-
public void printLine(List<Boolean> line) {
11-
StringBuilder output = new StringBuilder();
12-
output.append("|");
12+
private LadderOutputView() {
13+
}
14+
15+
private static final LadderOutputView ladderOutputView = new LadderOutputView();
1316

14-
for(boolean isConnected : line) {
15-
output.append(getJoiner(isConnected));
16-
output.append("|");
17+
public static LadderOutputView getInstance() {
18+
return ladderOutputView;
19+
}
20+
21+
public void printResultHeader() {
22+
System.out.println("실행결과");
23+
System.out.println();
24+
}
25+
26+
public void printLine(LineDto lineDto) {
27+
StringBuilder output = new StringBuilder()
28+
.append(INDENTATION)
29+
.append(PILLAR);
30+
31+
for (boolean isExist : lineDto.getLinkExistCollection()) {
32+
String coupler = getCoupler(isExist);
33+
output.append(coupler)
34+
.append(PILLAR);
1735
}
1836

1937
System.out.println(output);
2038
}
2139

22-
private String getJoiner(boolean isConnected) {
23-
if(isConnected) {
40+
private String getCoupler(boolean isLinkExist) {
41+
if (isLinkExist) {
2442
return DASH_COUPLER;
2543
}
2644
return BLANK_COUPLER;
2745
}
2846

29-
30-
3147
}

0 commit comments

Comments
 (0)