Skip to content

두 번째 버전 (Factory pattern)#3

Open
ca1af wants to merge 6 commits intoCODE-CLEANERS:mainfrom
ca1af:DK_V2
Open

두 번째 버전 (Factory pattern)#3
ca1af wants to merge 6 commits intoCODE-CLEANERS:mainfrom
ca1af:DK_V2

Conversation

@ca1af
Copy link
Collaborator

@ca1af ca1af commented Oct 16, 2023

No description provided.

Copy link
Collaborator Author

@ca1af ca1af left a comment

Choose a reason for hiding this comment

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

  1. 쓸모없는 클래스를 정리해라 (V1 류)
  2. @ParamaterizedTest 를 사용해라

Comment on lines +23 to +31
void invalid_lineInput_test() {
String input = "(1,1)1(5,5)";
String input2 = "(1,1)";
String input3 = "(25,25)-(25,25)";

assertThatThrownBy(() -> new Dots(input)).isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> new Dots(input2)).isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> new Dots(input3)).isInstanceOf(IllegalArgumentException.class);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@ParameterizedTest 써야한다.

Copy link

@dhlee128 dhlee128 left a comment

Choose a reason for hiding this comment

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

동균님 고생하셨습니다, 항상 코드 보면서 문법적인 부분에서 많이 배웁니다...
일단은 한번 가볍게 코멘트를 했는데 사실 문제가 있다고 생각할 부분이 없어서요. (코멘트라 하기에도 부족한 ㅠㅠ)
추후에 또 시간이된다면 다시 코멘트 하겠습니다 😊

Comment on lines +17 to +20
@Override
public List<Dot> getDots(){
return this.dotList;
}

Choose a reason for hiding this comment

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

getDots 도 함께 인터페이스에서 선언을 해준 이유가 무엇인가요?
AbstractFigure 클래스를 상속받는 클래스가 모두 사용할 거라면, 인터페이스에 선언하지 않고 추상클래스에서 바로 작성을 해도 될것같아서요!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

getDots 의 경우 팩토리패턴 예제(원본 깃헙 레포) 를 차용해서 해당 방식과 같이 진행했습니다 ㅎㅎ 다희님 말씀대로 추상클래스에서 바로 선언해도 될 것 같습니다!

Choose a reason for hiding this comment

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

22-25라인 대신 Dot(intx, inty) 호출을 해도 괜찮지 않을까요?
동일한 코드라서 테스트 시에 중복은 어쩔 수 없으니까요,,

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

저도 이 부분이 궁금해서 테스트 해봤는데, 생성자 매소드 특성상 불가능 한 것 같더라구요!

참고한 블로그 링크 드립니다

Choose a reason for hiding this comment

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

제가 잘 이해를 못 했습니다! 링크대로 this 로 다른 생성자를 호출할 수 있는데, 동균님이 말씀하신 '생성자 메서드 특성한 불가능하다' 는 어떤 의미인가요!?

Copy link
Collaborator Author

@ca1af ca1af Oct 26, 2023

Choose a reason for hiding this comment

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

public Dot(int x, int y) {
        validateLocations(x, y);
        this.x = x;
        this.y = y;
    }
  • 해당 생성자 매서드를 호출 할 때 우리는 new Dot() 과 같은 방식으로 호출하게 됩니다!
  • 해당 키워드(new)는 새로운 인스턴스를 만들어주는 키워드입니다.

그렇다면 우리는 두 가지 옵션이 생기게 됩니다.

  1. new Dot() 을 리턴한다?
image

위와 같이 생성자 매서드는 리턴 타입을 가지지 않는 매소드라서, 해당 방식은 불가능합니다.

  1. 그렇다면 이미 만들어진 인스턴스를 이용한다
image

위와 같은 방식은 사용하지 않는 인스턴스를 (new Dot) 만들게 되는 부작용이 있습니다.

  1. 그렇다면 정적 팩토리!?
image

해당 방식으로는 가능합니다 ㅎㅎ

이 경우는 아마도 생성자 매서드가 아닌, 정적 팩토리 매서드를 사용해서 아래와 같은 클래스 형태로 만들면 예쁜 것 같습니다. 수정 후 반영하겠습니다!

정적 팩토리 매서드 예시는 아래와 같습니다!

     private Dot(int x, int y) {
        validateLocations(x, y);
        this.x = x;
        this.y = y;
    }
    public static Dot ofTest(int x, int y){
        return new Dot(x,y);
    }
    public static Dot of(String input){
        String[] parts = input.replaceAll(REGEX_FOR_STRING_INPUT, "").split(",");
        if (parts.length != 2) {
            throw new IllegalArgumentException(INVALID_INPUT_FORMAT);
        }
        int x = Integer.parseInt(parts[0].trim());
        int y = Integer.parseInt(parts[1].trim());
        return new Dot(x, y);
    }

위와 같은 형태입니다. 😃 좀 더 예쁜 코드가 된 것 같습니다! 피드백 감사합니다 👍

Choose a reason for hiding this comment

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

사용하지 않는 클래스인가요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

팩토리 패턴 예제를 위해 만들어둔 클래스입니다!

해당 클래스를 사용해서 Figure를 상속하는 인스턴스를 "길이" 라는 식별자에 따라 리턴해 줄 수 있습니다. (Map의 Key 부분)

예를 들어 유저가 사용하는 측에서는

  1. 값을 입력한다 (String)
  2. 값을 적절히 List 으로 변환한다.
  3. List 의 길이에 따라서 어떤 shape를 사용자가 골랐는지 판단해서, 해당 shape 를 리턴한다.

이 패턴을 사용했을 때의 장점은 구현하는 측에서 어떤 구현체가 와야 하는지 정해주지 않아도 된다는 것입니다. 런타임 시점에 사용자가 입력한 값에 따라, 팩토리에 들어있는 인스턴스들을 리턴해 줄 수 있기 때문입니다!

일반적으로 상위 추상이 존재하며
해당 추상의 구현체 클래스들이 여러개인 경우에

해당 패턴을 사용하는 듯 합니다.

Choose a reason for hiding this comment

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

만약 여기서 사용한다면 key:2 =>Line, key:3=>Triangle .. 인스턴스를 반환을 해주는 팩터리 클래스 인거죠..
사용한다면 해당 클래스를 한번 생성한뒤에 길이를 꺼낼때 사용을 하면 되겠죠? (이해한게 맞나 싶지만, 일요일에 직접 보고싶네요 🤣)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

정확합니당!! ㅎㅎ 해당 방식으로 Map<> 을 사용하는 방식은 Spring으로 개발할때도 자주 쓰이는 패턴이라고 합니다!

Comment on lines +31 to +37
List<Double> collect = IntStream.range(0, 4)
.mapToObj(i -> getDot(i).getDistanceBetween(getDots().get((i + 1) % 4)))
.distinct().collect(Collectors.toList());

if (collect.size() == 1) return (int) Math.pow(collect.get(0), 2);

return collect.get(0) * collect.get(1);

Choose a reason for hiding this comment

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

🙌

Copy link

@dhlee128 dhlee128 left a comment

Choose a reason for hiding this comment

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

리뷰 답변 궁금증

Choose a reason for hiding this comment

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

만약 여기서 사용한다면 key:2 =>Line, key:3=>Triangle .. 인스턴스를 반환을 해주는 팩터리 클래스 인거죠..
사용한다면 해당 클래스를 한번 생성한뒤에 길이를 꺼낼때 사용을 하면 되겠죠? (이해한게 맞나 싶지만, 일요일에 직접 보고싶네요 🤣)

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