봄의 전략 디자인 패턴
엔터프라이즈 애플리케이션에서는 비즈니스 요구 사항에 따라 다르게 구현됩니다. 런타임 시 특정 조건에 따라 이들 중 하나를 실행해야 합니다. 일반적인 예로는 정렬 알고리즘, 데이터 암호화, 알림 서비스, 인증 서비스, 결제 서비스 등이 있습니다.
if-else 조건을 사용하는 대신 전략 설계 패턴을 사용하여 구현할 수 있습니다. 전략 디자인 패턴은 런타임에 다양한 구현을 실행/사용하는 데 도움이 됩니다. 전략은 코드 재사용성, 유연성, 관심사 분리 및 확장성
을 제공합니다.다양한 채널/클라이언트로부터 요청을 받는 통합 인증 서비스를 개발했다고 가정해 보세요. 런타임 시 구성에 따라 인증 목적으로 사용되는 구현을 결정해야 합니다(이는 전달 요청일 수도 있음). 구현은 okta, Azure Ad 또는 자체 IAM일 수 있습니다.
참고: 공급자를 독립 모듈로 구현하고 이를 메인 프로젝트에 종속성으로 추가해야 합니다.
전략 인터페이스
public interface Authentication { AuthenticationRes authentication(AuthenticationReq authenticationReq); OtpDevicesRes otpDevices(OtpDevicesReq otpDevicesReq); SendOtpRes sendOtp(SendOtpReq sendOtpReq); VerifyOtpRes verifyOtp(VerifyOtpReq verifyOtpReq); }
구체적인 전략
@Component("oktaAuthentication") @Slf4j public class OktaAuthentication implements Authentication { -------------- -------------- -------------- } @Component("ferAuthentication") @Slf4j public class FerAuthentication implements Authentication { -------------- -------------- -------------- } @Component("eapAuthentication") @Slf4j public class EapAuthentication implements Authentication { -------------- -------------- -------------- }
서비스
@Service @Slf4j public class AuthenticationService { public Map<String, Authentication> authenticationProvidersMap; public Set<String> availableAuthProviders; public AuthenticationService(Map<String, Authentication> authenticationProvidersMap) { this.authenticationProvidersMap = authenticationProvidersMap; this.availableAuthProviders = this.authenticationProvidersMap.keySet(); log.info("Available Auth providers:{}", this.availableAuthProviders); } public AuthenticationRes getAuthentication(AuthenticationReq authenticationReq, ClientDetails clientDetails) { //This method will identify authentication provider based on client details // and returns oktaAuthentication/eapAuthentication/ferAuthentication String authProvider = getAuthProviderDetails(clientDetails); if (this.availableAuthProviders.contains(authProvider)) { return this.authenticationProvidersMap.get(authProvider) .authentication(authenticationReq); } else { throw new AuthProviderUnavailable(authProvider); } } public String getAuthProviderDetails(ClientDetails clientDetails) { // implement your business logic to return the provider that need to be used. } }
질문이 있으시면 댓글로 질문을 남겨주세요.
위 내용은 봄의 전략 디자인 패턴의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

일부 애플리케이션이 제대로 작동하지 않는 회사의 보안 소프트웨어에 대한 문제 해결 및 솔루션. 많은 회사들이 내부 네트워크 보안을 보장하기 위해 보안 소프트웨어를 배포 할 것입니다. ...

많은 응용 프로그램 시나리오에서 정렬을 구현하기 위해 이름으로 이름을 변환하는 솔루션, 사용자는 그룹으로, 특히 하나로 분류해야 할 수도 있습니다.

시스템 도킹의 필드 매핑 처리 시스템 도킹을 수행 할 때 어려운 문제가 발생합니다. 시스템의 인터페이스 필드를 효과적으로 매핑하는 방법 ...

IntellijideAultimate 버전을 사용하여 봄을 시작하십시오 ...

데이터베이스 작업에 MyBatis-Plus 또는 기타 ORM 프레임 워크를 사용하는 경우 엔티티 클래스의 속성 이름을 기반으로 쿼리 조건을 구성해야합니다. 매번 수동으로 ...

Java 객체 및 배열의 변환 : 캐스트 유형 변환의 위험과 올바른 방법에 대한 심층적 인 논의 많은 Java 초보자가 객체를 배열로 변환 할 것입니다 ...

Redis 캐싱 솔루션은 제품 순위 목록의 요구 사항을 어떻게 인식합니까? 개발 과정에서 우리는 종종 a ... 표시와 같은 순위의 요구 사항을 처리해야합니다.

전자 상거래 플랫폼에서 SKU 및 SPU 테이블의 디자인에 대한 자세한 설명이 기사는 전자 상거래 플랫폼에서 SKU 및 SPU의 데이터베이스 설계 문제, 특히 사용자 정의 판매를 처리하는 방법에 대해 논의 할 것입니다 ...
