1. μ λ΅ ν¨ν΄μ΄λ
μ λ΅ ν¨ν΄μ μκ³ λ¦¬μ¦μ κ·Έλ£ΉμΌλ‘ μ μνκ³ , κ° κ·Έλ£Ήμ λ³λμ ν΄λμ€μμ κ΄λ¦¬νλ©° κ·Έλ£Ήμ μκ³ λ¦¬μ¦μ μ νμ μΌλ‘ μ¬μ©ν μ μλλ‘ νλ νλ λμμΈν¨ν΄μ΄λ€.
μ€νλ§ νλ μμν¬ λ° JAVA API λ΄μμλ λΉλ²νκ² μ°Ύμλ³Ό μ μλ λμμΈ ν¨ν΄μ΄λ€.
κ·Έλ¦¬κ³ μ€νλ§μ μ¬μ©νκ³ μλ€λ©΄ λΉ μ»¨ν μ΄λλ₯Ό μ¬μ©νμ¬ ν΄λΉ μ λ΅ν¨ν΄μ κΉλνκ² μ μ©ν μ μλλ° νΉμ μλΉμ€μ ν΄λμ€λ λ©μλκ° κ³Όλνκ² λ¬΄κ±°μ μ§λκ²μ μλ°©ν μ μλ€.
2. μ μ© μμ
κΈ°μ‘΄μ νμ¬μμ μ§μνλ κ²°μ μλ¨μ΄ λ§μ§ μμλλ°, μ΄λ²μ μ κ· μ± λ°μΉκ³Ό λλΆμ΄ λ€μν κ²°μ μλ¨μ μ§μνκ² λμλ€.
μ μ° λͺ¨λλ κ·Έμ λ§μΆμ΄ μ¬λ¬ κ²°μ μλ¨μ λ°λ₯Έ λΉμ§λμ€ λ‘μ§μ μννλλ‘ λ³κ²½ν΄μΌ νλ μν©μ΄μλ€.
κΈ°μ‘΄μ μ§μνλ κ²°μ μλ¨μ νΈμμ A, B, C λΌκ³ μΉνκ² λ€.
μ΅μ΄ κ²°μ μλ¨μ λλ¦¬λ €λ μμ μμλ λ¨μνκ² λΆκΈ°λ¬Έμ ν΅ν΄ A,B,C κ²°μ μλ¨μ λΆκΈ°νλλ‘ μμ±λμ΄ μμλ€.
@Service
@RequiredArgsConstructor
public class purchaseService {
private final PaymentMethodAService aService;
private final PaymentMethodBService bService;
private final PaymentMethodCService cService;
public Result purchase(PurchaseInfo purchaseInfo) {
PaymentMethod paymentMethod = purchaseInfo.getPaymentMethod();
if (paymentMethod == PaymentMethod.A) {
return aService.pay(purchaseInfo);
} else if (paymentMethod == PaymentMethod.B) {
return bService.pay(purchaseInfo);
} else if (paymentMethod == PaymentMethod.C) {
return cService.pay(purchaseInfo);
} else {
throw new IllegalArgumentException("payment method not found");
}
}
public Result refund(RefundInfo refundInfo) {
PaymentMethod paymentMethod = refundInfo.getPaymentMethod();
if (paymentMethod == PaymentMethod.A) {
return aService.refund(refundInfo);
} else if (paymentMethod == PaymentMethod.B) {
return bService.refund(refundInfo);
} else if (paymentMethod == PaymentMethod.C) {
return cService.refund(refundInfo);
} else {
throw new IllegalArgumentException("payment method not found");
}
}
}
μ μ½λλ₯Ό 보면 μ°μ μΌν λ΄λ μΈ κ°μ§ μ λμ λ¬Έμ μ μ΄ λ³΄μΈλ€.
- κ²°μ μλ¨μ΄ μΆκ° λλ κ²½μ° κ³ μΉ μ½λκ° λ무 λ§λ€.
- κ²°μ κ΄λ ¨ κΈ°λ₯μ΄ μΆκ°λλ κ²½μ° κ³ μΉ μ½λκ° λ무 λ§λ€.
- κ²°μ μλ¨μ΄ λμ΄λ λ λ§λ€ μμ‘΄μ± λν λμ΄λλ€. (λ¨μν μ€νΈ μ λΆνμν μ½λμμ± μκ° μ¦κ°)
μ΄μ λ€μ μ½λλ₯Ό μ λ΅ ν¨ν΄μ μ μ©ν¨μΌλ‘μ¨ λ¬Έμ μ μ ν΄κ²°ν΄λ³΄μ.
3. μ μ©
μ°μ 곡ν΅μ μΌλ‘ κ²°μ
, νλΆ
λ± ν΅μ¬μ μΈ λ΄μ©μ μΆμν νμ¬ μΈν°νμ΄μ€λ‘ λ§λ λ€.
public interface PaymentMethodService {
Result purchase(PurchaseInfo purchaseInfo);
Result refund(RefundInfo refundInfo);
}
κ·Έλ¦¬κ³ κ²°μ μλ¨λ€μ΄ ν΄λΉ μΈν°νμ΄μ€λ₯Ό μμλ°λλ‘ λ³κ²½νλ€.
@Service("AMethod")
public class PaymentMethodAService implements PaymentMethodService {
//...
}
@Service("BMethod")
public class PaymentMethodBService implements PaymentMethodService {
//...
}
λ€μκ³Ό κ°μ΄ μ¬λ¬ ν΄λμ€κ° μΈν°νμ΄μ€λ₯Ό μμ λ°μΌλ©΄ Bean μ£Όμ
μ `Map` μ΄λ `List` μ κ°μ 컬λ μ
ννλ‘ κ°μ²΄λ€μ κ°μ Έμ¬ μ μλ€.
λ°λΌμ μ²μ μ½λλ₯Ό λ€μκ³Ό κ°μ΄ μμ κ°λ₯νλ€.
@Service
@RequiredArgsConstructor
public class purchaseService {
private final Map<String, PaymentMethodService> paymentMethodServiceMap;
public Result purchase(PurchaseInfo purchaseInfo) {
String paymentMethod = purchaseInfo.getPaymentMethod().getName();
return paymentMethodServiceMap.computeIfAbsent(paymentMethod, key -> {
throw new IllegalArgumentException("payment method not found");
}).pay(purchaseInfo);
}
public Result refund(RefundInfo refundInfo) {
String paymentMethod = refundInfo.getPaymentMethod().getName();
return paymentMethodServiceMap.computeIfAbsent(paymentMethod, key -> {
throw new IllegalArgumentException("payment method not found");
}).refund(refundInfo);
}
}
λ€μκ³Ό κ°μ΄ μ λ΅ ν¨ν΄μ μ μ© ν¨μΌλ‘μ¨ μμ λ€λ£¬ 3κ°μ§ λ¬Έμ μ μ΄ κΉλνκ² ν΄κ²°λμλ€.
μ΄ μ΄ν κ²°μ μλ¨μ΄ κ³μ μΆκ°λμ΄λ, κΈ°μ‘΄ μ½λμ μμ μμ΄ PaymentMethodService
λ₯Ό μμλ°λ ν΄λμ€λ§ μΆκ°ν¨μΌλ‘μ¨ μμ νκ³ νΈλ¦¬ν νμ₯μ΄ κ°λ₯ ν΄μ‘λ€. π