Skip to content

Commit

Permalink
♻️ Refactor: OCP를 준수하는 방향으로 전략 패턴 코드 리팩토링 (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
swa07016 authored Dec 26, 2023
1 parent a227e3d commit f20e618
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
@RequiredArgsConstructor
public class BriefingQueryService {

private final BriefingQueryContextFactory briefingQueryContextFactory;

public List<Briefing> findBriefings(BriefingRequestParam.BriefingPreviewListParam params, APIVersion version) {
BriefingQueryContext briefingQueryContext = BriefingQueryContextFactory.getContextByVersion(version);
BriefingQueryContext briefingQueryContext = briefingQueryContextFactory.getContextByVersion(version);
return briefingQueryContext.findBriefings(params);
}

public Briefing findBriefing(final Long id, final APIVersion version) {
BriefingQueryContext briefingQueryContext = BriefingQueryContextFactory.getContextByVersion(version);
BriefingQueryContext briefingQueryContext = briefingQueryContextFactory.getContextByVersion(version);
return briefingQueryContext.findById(id)
.orElseThrow(() -> new BriefingException(ErrorCode.NOT_FOUND_BRIEFING));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,33 @@
import briefing.common.enums.APIVersion;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.EnumMap;
import java.util.List;
import java.util.Map;

@Component
@RequiredArgsConstructor
public class BriefingQueryContextFactory {

private final BriefingRepository briefingRepository;
private static BriefingQueryContext staticBriefingQueryContextV1;
private static BriefingQueryContext staticBriefingQueryContextV2;

@PostConstruct
private void init() {
staticBriefingQueryContextV1 = createContext(new BriefingV1QueryStrategy(briefingRepository));
staticBriefingQueryContextV2 = createContext(new BriefingV2QueryStrategy(briefingRepository));
}
private final Map<APIVersion, BriefingQueryContext> contextMap;

private static BriefingQueryContext createContext(BriefingQueryStrategy strategy) {
return new BriefingQueryContext(strategy);
@Autowired
public BriefingQueryContextFactory(List<BriefingQueryStrategy> strategies) {
contextMap = new EnumMap<>(APIVersion.class);
for (BriefingQueryStrategy strategy : strategies) {
APIVersion version = strategy.getVersion();
contextMap.put(version, new BriefingQueryContext(strategy));
}
}

public static BriefingQueryContext getContextByVersion(APIVersion version) {
return switch (version) {
case V1 -> staticBriefingQueryContextV1;
case V2 -> staticBriefingQueryContextV2;
};
public BriefingQueryContext getContextByVersion(APIVersion version) {
BriefingQueryContext context = contextMap.get(version);
if (context == null) {
throw new IllegalArgumentException("Invalid API version: " + version);
}
return context;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import briefing.briefing.application.dto.BriefingRequestParam;
import briefing.briefing.domain.Briefing;
import briefing.common.enums.APIVersion;

import java.util.List;
import java.util.Optional;
Expand All @@ -10,4 +11,6 @@ public interface BriefingQueryStrategy {
List<Briefing> findBriefings(BriefingRequestParam.BriefingPreviewListParam params);

Optional<Briefing> findById(Long id);

APIVersion getVersion();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
import briefing.briefing.domain.Briefing;
import briefing.briefing.domain.BriefingType;
import briefing.briefing.domain.repository.BriefingRepository;
import briefing.common.enums.APIVersion;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Component
@RequiredArgsConstructor
public class BriefingV1QueryStrategy implements BriefingQueryStrategy {

Expand All @@ -34,4 +37,9 @@ public List<Briefing> findBriefings(BriefingRequestParam.BriefingPreviewListPara
public Optional<Briefing> findById(Long id) {
return briefingRepository.findById(id);
}

@Override
public APIVersion getVersion() {
return APIVersion.V1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import briefing.briefing.application.dto.BriefingRequestParam;
import briefing.briefing.domain.Briefing;
import briefing.briefing.domain.repository.BriefingRepository;
import briefing.common.enums.APIVersion;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Component
@RequiredArgsConstructor
public class BriefingV2QueryStrategy implements BriefingQueryStrategy{

Expand All @@ -35,4 +38,9 @@ public List<Briefing> findBriefings(BriefingRequestParam.BriefingPreviewListPara
public Optional<Briefing> findById(Long id) {
return briefingRepository.findByIdWithScrapCount(id);
}

@Override
public APIVersion getVersion() {
return APIVersion.V2;
}
}

0 comments on commit f20e618

Please sign in to comment.