Skip to content

[ 사다리 - 함수형 프로그래밍 ] 박채연 미션 제출합니다. #57

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
wants to merge 2 commits into
base: qkrcodus
Choose a base branch
from

Conversation

qkrcodus
Copy link

  • enum
  • 일급 컬렉션
  • 원시값 포장하기가 프로그램의 요구 사항이여서 우선 이들이 언제 사용되고 사다리에선 어떻게 쓰이면 좋을지 다시 공부 해보았습니다.

[ enum ] - 고정된 상태에 따른 행동/속성 정의하고 싶을 때 사용

  • enum은 클래스다.
  • 클래스는 사용자 정의 타입이다.
  • 클래스로부터 생성될 수 있는 인스턴스를 1개로 제한해도 되는, 즉 컴파일 타임에 필요한 인스턴스를 전부 알고 있는 경우 enum으로 표현 가능하다.
  • enum은 여느 클래스와 마찬가지로 필드, 메서드, 생성자를 가질 수 있으며, 각 상수마다 서로 다른 속성과 동작을 정의할 수도 있다.

[ 일급 컬렉션 ] - 컬렉션이 도메인 의미 갖고 있을 때 사용

  • List, Set 같은 컬렉션을 필드로 갖는 객체다.
  • 이 객체는 다른 멤버 변수가 없어야 한다.
  • 컬렉션을 wrapping 해서 이름을 부여할 수 있고, 유효성 검사등 행위도 부여할 수 있다.

Enum 과 일급 컬렉션은 둘 다 정보 은닉을 목적으로 한다.


[ 원시값 포장 ] - 원시값이 도메인 의미 갖고 있을 때 사용

  • Collection 으로 선언한 변수 포장 -> 일급 컬렉션
  • 필드에 놓이는 primitive 타입을 포장 -> 검증 로직 객체 안에 숨길 수 있다.
  • 장점 : 도메인 의미를 갖거나, 검증이 필요하거나, 여러 곳에서 사용되는 경우 의미 있다.
  • 단점 : 오버라이딩 필요해지고, 클래스가 과도하게 많아질 수 있다.

사다리 구성요소가 무엇인지 생각하며 어떤 클래스들이 필요할지 고민했습니다.

Ladder -> Floor -> Connection
Ladder : List < Floor > - 일급 컬렉션
Floor : List < Connection > - 일급 컬렉션
Connection : boolean isConnected, public static Connection.of(boolean) - 정적 팩토리 메서드 (생성 제약 있음)
원시값 포장이 필요한 부분은 없는 것 같다.

  • | width 개와 중간 연결 지점 width-1개 : Floor
  • Floor height개 : Ladder
  • 연결 지점 랜덤으로 단, 연속 불가 : Connection

3단계 게임 결과 출력하기

  • 사다리의 결과를 계산하는 역할을 하는 LadderResult을 추가해줬습니다.
  • 한 층인 Floor는 ----- 연결 여부에 대한 정보가 있으므로, floor에게 연결 여부를 묻고 오른쪽으로 연결 되어있다면 index+1, 왼쪽으로 연결되어 있다면 index-1 해줍니다.
  • 사다리의 각 층인 Floor를 차례로 내려가며 현재 위치인 position을 갱신해주며 아래층까지 내려옵니다.

정리

  • Connectiion : ----- 연결 여부 판단, 이전에 연결되었는진 Floor에게 물어본다.
  • Floor : 각 층마다 ----- 연결 여부의 조합
  • Ladder : Floor height개 조립, 각층의 형태는 Floor에게 물어본다.
  • OutputView : 사다리 출력
  • LadderController : view정보를 model 에게 전달, model의 상태를 view에게 전달
  • LadderApplication : 조립
  • LadderResult : 사다리 게임 후 위치 계산

Copy link

@RIANAEH RIANAEH left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

채연님 도메인 파악부터 시작해야해서 미션이 까다로웠을텐데 잘 구현해주셨네요!
객체별 역할을 구분하는 연습을 하시는 모습 좋습니다👍

현재 어플리케이션을 수행해보면 IndexOutOfBoundsException이 발행하는데요.
이부분 수정 부탁드리며, 테스트 코드도 작성하면서 기능이 잘 구현되었는지 그때 그때 검증해보며 진해앻보면 좋을 것 같습니다! 홧팅🔥

Comment on lines +7 to +8
public class Ladder {
private List<Floor> floors;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사다리는 N개의 층을 가질 수 있고, 층은 N개의 연결여부를 가지고 있게 구성해주셨군요! 좋습니다😎

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List는 변경될 수 없음을 보장하기 위해서는 어떻게 해야할까요?

Comment on lines +10 to +20
public Ladder(int height, int width, Random random) {
this.floors = generateFloors(height, width, random);
}

private List<Floor> generateFloors(int height, int width, Random random) {
List<Floor> floors = new ArrayList<>();
for (int i = 0; i < height; i++) {
floors.add(new Floor(width, random));
}
return floors;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List floors를 넣어주는 생성자를 만들어주고, 높이와 넓이 connection 생성전략을 넣어주는 정적 팩토리메서드를 만들어주면 어떨까요?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

높이와 넓이에 대한 검증도 있으면 좋겠네요!

Comment on lines +10 to +18
public Floor(int width,Random random) {
connections = new ArrayList<>();
boolean previousConnection = false;
for (int i = 0; i < width-1; i++) {
Connection connection =Connection.of(previousConnection,random);
connections.add(connection);
previousConnection = connection.isConnected();
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List를 넣어주는 생성자를 만들어주고, 넓이와 connection 생성전략을 넣어주는 정적 팩토리메서드를 만들어주면 어떨까요?

생성자에서는 어떤것을 검증해볼 수 있을까요? (Floor가 어떤 규칙을 가져야하는지 생각해보면?)

Comment on lines +5 to +15
public class Connection {
private final boolean connected;

private Connection(boolean connected) {
this.connected = connected;
}

public static Connection of(boolean previousConnected, Random random) {
boolean connect = !previousConnected && random.nextBoolean();
return new Connection(connect);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이건 선택의 영역이지만 Floor 내의 커낵션을 결정하는 로직은 Floor가 가지고 있어도 무방하다고 생각하긴 합니다!
그래도 값들을 최대한 값객체로 만들어보려는 시도 좋습니다👍

Comment on lines +30 to +35
public void printResult(LadderResult result, int width) {
for (int i = 0; i < width; i++) {
int end = result.move(i);
System.out.println(i + " -> " + end);
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

누구의 역할일까요?

Comment on lines +3 to +4
public class LadderResult {
private final Ladder ladder;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LadderResult라는 이름을 가지고 있지만, Ladder 내에서의 움직임 정책을 담당하고 있는 것 같아요.
움직임을 수행시키고 결과를 만들어 반환하는 객체로 바꿔보면 좋을 것 같고, LadderResult는 정말 결과만 들고 있으면 좋을 것 같네요!

}

private int movePosition(Floor floor, int index) {
if (floor.getConnections().get(index).isConnected()) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

floor에게 물어볼 수 있지 않을까요?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants