1Cuối tháng, client duyệt ba giao dịch mẫu — chuyển khoản VCB, nạp tiền điện thoại, hoá đơn EVN — và gọi tx.accept(feeCalculator) cho từng cái, không tự phân loại giao dịch.At month end, the client walks three sample transactions — a VCB transfer, a phone top-up, an EVN bill — and calls tx.accept(feeCalculator) on each, without classifying them itself.
2Giao dịch đầu là Transfer nên runtime chạy Transfer.accept (dispatch lần một). accept gọi v.visitTransfer(this), runtime chọn triển khai của FeeCalculator (dispatch lần hai) — không cần ép kiểu.The first transaction is a Transfer, so the runtime runs Transfer.accept (first dispatch). accept calls v.visitTransfer(this), and the runtime picks FeeCalculator's implementation (second dispatch) — no cast needed.
3visitTransfer: toBank là VCB, khác OWN, nên trả INTERBANK_FEE = 3300. Kết quả đi qua accept trở về client để cộng dồn.visitTransfer: toBank is VCB, not OWN, so it returns INTERBANK_FEE = 3300. The result travels back through accept to the client for summing.
4Giao dịch thứ hai là TopUp: TopUp.accept gọi visitTopUp(this) và FeeCalculator trả BigDecimal.ZERO vì nạp tiền miễn phí.The second is a TopUp: TopUp.accept calls visitTopUp(this) and FeeCalculator returns BigDecimal.ZERO because top-ups are free.
5Giao dịch thứ ba là BillPayment: accept gọi visitBill(this), FeeCalculator tính amount × BILL_RATE = 500000 × 0,001 = 500. Tổng phí 3.800đ; mọi quy tắc phí nằm cùng một chỗ.The third is a BillPayment: accept calls visitBill(this), and FeeCalculator computes amount × BILL_RATE = 500000 × 0.001 = 500. Total fees are 3,800 VND; every fee rule lives in one place.
6Thao tác mới, không sửa class giao dịch: ExportCSV trong Go tạo var e CSVExporter rồi gọi t.Accept(&e) cho từng giao dịch. Mỗi VisitXxx ghi một dòng CSV vào strings.Builder.A new operation with no change to the transaction types: Go's ExportCSV creates var e CSVExporter and calls t.Accept(&e) on each transaction. Each VisitXxx writes one CSV row to a strings.Builder.
7Phương án Java 21: StatementLine.describe(tx) dùng switch pattern matching trên sealed Transaction, không cần accept; thiếu một case là lỗi biên dịch vì không có default.The Java 21 alternative: StatementLine.describe(tx) uses a pattern-matching switch over the sealed Transaction, with no accept; a missing case is a compile error because there is no default.
8Phương án Go: Fee(t) dùng type switch t.(type) và tính cùng kết quả 3300 / 0 / 500. Go không kiểm tra đủ case lúc biên dịch, nên nhánh default panic khi gặp kiểu lạ.The Go alternative: Fee(t) uses a t.(type) type switch and computes the same 3300 / 0 / 500. Go does not check exhaustiveness at compile time, so the default branch panics on an unknown type.
9Đánh đổi: thêm thao tác (CSVExporter) rất rẻ, nhưng thêm kiểu QrPayment thì Visitor phải có visitQr và cả FeeCalculator lẫn CSVExporter phải bổ sung — hợp khi tập kiểu ổn định, thao tác thay đổi.Trade-off: adding an operation (CSVExporter) is cheap, but adding a QrPayment type means Visitor needs visitQr and both FeeCalculator and CSVExporter must add it — a fit when the type set is stable and operations change.