BananMoon / TIL

TIL : Today I Learned
0 stars 0 forks source link

[팩토리 패턴] 추상 클래스의 구현체 클래스를 상황에 따라 호출할 수 있다. #10

Open BananMoon opened 2 months ago

BananMoon commented 2 months ago
import org.springframework.stereotype.Component;

@Component
public class PerformanceServiceFactory {

    private final PerformanceService concertService;
    private final PerformanceService exhibitionService;

    public PerformanceServiceFactory(ConcertService concertService, ExhibitionService exhibitionService) {
        this.concertService = concertService;
        this.exhibitionService = exhibitionService;
    }

    public PerformanceService getServiceByUri(String uri) {
        if (uri.startsWith("/reserve/concert")) {
            return concertService;
        } else if (uri.startsWith("/reserve/exhibition")) {
            return exhibitionService;
        } else {
            throw new IllegalArgumentException("Unknown URI: " + uri);
        }
    }
}