1Trước khi nhận đơn, ứng dụng đăng ký CARD và MOMO vào registry. Spring tiêm List<PaymentStrategy> rồi CheckoutService gom thành Map theo method(); Go khai báo map trong NewCheckout(phone).Before taking orders, the application registers CARD and MOMO. Spring injects List<PaymentStrategy> and CheckoutService collects it into a Map keyed by method(); Go declares the map in NewCheckout(phone).
2Khách chọn MoMo cho đơn 100.000đ; client (ví dụ controller) gọi checkout("MOMO", 100000). Lựa chọn đến từ request, nên cùng luồng checkout phục vụ được nhiều cách trả tiền.The customer chooses MoMo for a 100,000 VND order; the client (e.g. a controller) calls checkout("MOMO", 100000). The choice comes from the request, so one checkout flow serves several payment methods.
3Service gọi strategies.get("MOMO") và nhận MomoPayment, không cần rẽ nhánh if/switch. Key không tồn tại sẽ bị từ chối ngay.The service calls strategies.get("MOMO") and gets MomoPayment, with no if/switch branching. A missing key is rejected immediately.
4Service gọi strategy.pay(amount) qua PaymentStrategy vì nó chỉ cần hợp đồng thanh toán chung. Chi tiết gọi ví MoMo nằm ngoài checkout.The service calls strategy.pay(amount) through PaymentStrategy because it only needs the shared payment contract. MoMo wallet details stay outside checkout.
5Lời gọi chạy vào MomoPayment: bản Java gọi client.charge(amount) lấy txId rồi trả PaymentResult.ok("momo " + txId + ...); bản Go dùng số điện thoại 0909 nên trả chuỗi bên dưới.Dispatch runs MomoPayment: the Java version calls client.charge(amount) for a txId and returns PaymentResult.ok("momo " + txId + ...); the Go version uses phone 0909 and returns the string below.
6Đơn tiếp theo cũng 100.000đ nhưng khách chọn CARD. Client gửi lựa chọn mới vào cùng service, không phải đổi trạng thái dùng chung hay sửa luồng checkout.The next order is also 100,000 VND, but this customer chooses CARD. The client sends the new choice to the same service without changing shared state or rewriting checkout.
7Lần này registry trả về CardPayment, nhưng service vẫn gọi đúng pay() như trước. Chỉ đối tượng nhận việc thay đổi.This time the registry returns CardPayment, but the service still calls the same pay(). Only the recipient changes.
8CardPayment cộng phí 2%: 100000 + 2000, nên trả chuỗi bên dưới. Bản Java là một @Component implement PaymentStrategy, nên Spring tự đăng ký mà không phải sửa CheckoutService.CardPayment adds a 2% fee: 100000 + 2000, returning the string below. The Java version is a @Component implementing PaymentStrategy, so Spring registers it without touching CheckoutService.
9Một request gửi VNPAY chưa được đăng ký: Java ném IllegalArgumentException, Go trả lỗi. Muốn hỗ trợ VNPay chỉ cần thêm một implementation và đăng ký vào map; lời gọi pay() trong checkout giữ nguyên.A request for VNPAY, which is not registered: Java throws IllegalArgumentException, Go returns an error. Supporting VNPay only takes one more implementation registered in the map; the pay() call in checkout stays the same.