우리 시스템은 PaymentService 인터페이스를 사용하지만
외부 결제 업체는 다른 메소드 명과 구조를 가진 ExternalPaymentAPI를 제공하고 있는 경우 어떻게 해결 가능할까요?
Adapter 패턴을 통해 내부 코드와 외부 API를 연결하면 됩니다
아래가 외부 결제 업체가 제공하는 API의 일부라면
public class ExternalPaymentAPI {
public String makePayment(double money) {
if (money > 0) {
return "SUCCESS";
}
return "FAIL";
}
}
먼저 내부 시스템과 연결하기 위한 인터페이스 생성
public interface PaymentService {
boolean pay(int amount);
}
Adapter 구현체 생성
public class PaymentAdapter implements PaymentService {
private final ExternalPaymentAPI externalAPI;
public PaymentAdapter(ExternalPaymentAPI externalAPI) {
this.externalAPI = externalAPI;
}
@Override
public boolean pay(int amount) {
String result = externalAPI.makePayment((double) amount);
return "SUCCESS".equals(result);
}
}
수정 불가능한 레거시나 외부 서비스를 사용해야한다면
Adapter Pattern을 사용하여 해결하도록 하자
import org.springframework.stereotype.Service;
@Service
public class OrderService {
private final PaymentService paymentService;
public OrderService() {
//Adapter를 통해 외부 API와 연결
this.paymentService = new PaymentAdapter(new ExternalPaymentAPI());
}
public String processOrder(int amount) {
boolean paid = paymentService.pay(amount);
if (paid) {
return "주문 결제가 완료되었습니다.";
} else {
return "결제 실패!";
}
}
}
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/order")
public class OrderController {
private final OrderService orderService;
public OrderController(OrderService orderService) {
this.orderService = orderService;
}
@PostMapping("/pay")
public String pay(@RequestParam int amount) {
return orderService.processOrder(amount);
}
}'Etc' 카테고리의 다른 글
| PostgreSQL FDW (0) | 2025.10.12 |
|---|---|
| 로그파일 용량 초기화 (0) | 2025.10.05 |
| 간단한 SQL 튜닝 팁 (0) | 2025.09.28 |
| 결제 안정성 확보 (0) | 2025.09.21 |
| nofile이란? (0) | 2025.09.21 |