Skip to content
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

refactor [#223] ArtistResolver 객체 그래프 탐색 로직을 분리하기 위해 전략(Strategy) 패턴 적용 #224

Merged
merged 8 commits into from
Feb 14, 2025

Conversation

ch1hyun
Copy link
Collaborator

@ch1hyun ch1hyun commented Feb 13, 2025

✨ Issue Number ✨

closes #223

✨ To-do ✨

  • Artist 정보 수집 Registry 생성
  • ArtistResolver에서 Artist 정보 수집 로직 분리
  • 탐색 대상 엔티티 Registry 등록

✨ Description ✨  

문제 상황

기존 ArtistResolver 클래스는 탐색 대상 클래스의 Artist 정보를 수집하고, 조회된 정보를 주입하기 위해 단일 파일에 함수를 등록했습니다.
이로 인해 새로운 엔티티가 추가되거나 엔티티 변경사항이 생기면 ArtistResolver 파일도 수정해야하는, 변경지점이 다수 발생하는 문제가 있습니다.

[탐색 대상 클래스 등록]
Image

[객체 그래프 탐색 로직 구현]
Image

이를 해결하기 위해 전략(Strategy) 패턴을 사용해서 엔티티의 변경 사항이 ArtistResolver에 영향이 가지않도록 격리시키고 탐색이 필요한 엔티티를 Registry에 등록시켜 유연성과 확장성(OCP 원칙) 향상시키고자 합니다.

해결 방법

image

아티스트 정보를 주입받기 위해 구현해야 하는 함수를 강제한 인터페이스 입니다.

  • collect 함수 : 엔티티 객체 그래프 상의 아티스트 정보를 가지는 객체의 위치를 정의하는 함수입니다. 객체 그래프 상의 탐색 대상을 정의하고 artistMapper에 객체를 등록합니다.
  • supports 함수 : ArtistResolver에 주어진 엔티티의 클래스에 대응되는 전략을 반환하기 위해 해당 전략이 주어진 클래스를 지원하는지 여부를 반환합니다.

image

artistMapper 변수 관리를 위해 공통적인 함수 작성이 필요했습니다. 그래서 abstract 클래스를 작성했습니다.

image

ArtistResolver 가 사용하는 아티스트 전략 빈 클래스입니다.
주어진 타겟의 클래스를 가지고 적절한 전략을 요청합니다.

image

Festival 엔티티가 구현한 아티스트 전략 클래스입니다.
자신이 가지고 있는 아티스트 객체 위치를 탐색해 artistMapper 에 등록합니다.
Festival 클래스 타입일 경우 supports 함수는 true를 반환합니다.

[ArtistResolver load 함수]

변경 전 변경 후
image image

ArtistMapper를 지역변수로 변경한 이유는 싱글톤으로 관리되는 빈은 상태를 가지면 안되기 때문입니다. 상태를 가지면 서로 다른 스레드에서 처리하는 데이터에 서로 영향을 줄 수 있기 때문에 지역 변수로 변경했습니다.

[ArtistResolver collect 함수]

변경 전 변경 후
image image

코드 변경 후에는 아티스트 전략 구현 클래스만 변경하면 Resolver 클래스를 변경하지 않아도 변경사항을 적용할 수 있게 되었습니다.

@ch1hyun ch1hyun added the 🛠️ Refactor 코드 리팩토링 label Feb 13, 2025
@ch1hyun ch1hyun self-assigned this Feb 13, 2025
@ch1hyun ch1hyun requested a review from Ivoryeee February 13, 2025 11:16
Copy link
Collaborator

@Ivoryeee Ivoryeee left a comment

Choose a reason for hiding this comment

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

Strategy 패턴 도입의 근거가 명확한 상황에서 전략의 캡슐화가 잘된 코드인 것 같아요!
PR을 구체적으로 작성해 주신 덕분에 코드를 이해하는 데 수월했습니다! 수고 많으셨습니다!

@ch1hyun ch1hyun merged commit 2e68731 into develop Feb 14, 2025
1 check passed
@ch1hyun ch1hyun deleted the refactor#223 branch February 14, 2025 05:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🛠️ Refactor 코드 리팩토링
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[refactor] ArtistResolver 객체 그래프 탐색 로직을 분리하기 위해 전략(Strategy) 패턴 적용
2 participants