1Lúc khởi động, nơi lắp ráp tạo cài đặt OrderRepository (demo: InMemoryOrderRepository, Go: memory.NewOrderRepo(); production: postgres.NewOrderRepo(db)) và SmsNotifier với SmsGatewayClient đã cấu hình. API key và cách tạo bộ gửi nằm ở đây, để OrderService chỉ lo xử lý đơn.At startup, the assembly point creates an OrderRepository implementation (demo: InMemoryOrderRepository, Go: memory.NewOrderRepo(); production: postgres.NewOrderRepo(db)) and SmsNotifier with a configured SmsGatewayClient. API keys and sender construction live here so OrderService can focus on orders.
2Nơi lắp ráp truyền repository và bean @Qualifier("sms") vào constructor OrderService(orders, notifier) (Go: order.NewService(repo, n)). Service không bị gắn chặt với cách tạo bộ gửi SMS.The assembly point passes the repository and the @Qualifier("sms") bean into the OrderService(orders, notifier) constructor (Go: order.NewService(repo, n)). The service is decoupled from how the SMS sender is built.
3placeOrder(order) gọi orders.save(order) qua hợp đồng OrderRepository, rồi gọi notifier.send(order.phone(), ...) qua hợp đồng Notifier. Logic đơn chỉ biết hai interface.placeOrder(order) calls orders.save(order) through the OrderRepository contract, then notifier.send(order.phone(), ...) through the Notifier contract. The order logic only knows two interfaces.
4Object được truyền vào là SmsNotifier nên chính nó xử lý lời gọi và chuyển tiếp gateway.post(to, message). Interface là hợp đồng kiểu, không phải container đi tìm bean mỗi lần gửi.The injected object is an SmsNotifier, so it handles the call and forwards gateway.post(to, message). The interface is a type contract, not a container lookup on every send.
5Lần khởi động sau, ta chọn Zalo ở chỗ lắp ráp: đổi @Qualifier("sms") thành "zalo" trong Java, hoặc đặt NOTIFIER=zalo để main() gọi notify.NewZalo(ZALO_TOKEN) trong Go.For the next startup, pick Zalo at the assembly point: change @Qualifier("sms") to "zalo" in Java, or set NOTIFIER=zalo so main() calls notify.NewZalo(ZALO_TOKEN) in Go.
6Nơi lắp ráp tạo OrderService mới với ZaloNotifier. Constructor injection không tự thay dependency của service đang chạy; ta đang dựng một đồ thị object mới.The assembly point constructs a new OrderService with ZaloNotifier. Constructor injection does not replace a running service's dependency; this builds a new object graph.
7Đơn A1025 đi qua đúng các lời gọi cũ: orders.save() rồi notifier.send(). OrderService không có nhánh if SMS/Zalo vì lựa chọn đã quyết định lúc lắp ráp.Order A1025 follows the same calls: orders.save() then notifier.send(). OrderService has no SMS/Zalo branch because the choice was made during assembly.
8Lần này ZaloNotifier nhận lời gọi và gửi qua Zalo OA. Đổi cài đặt mà không sửa logic đặt đơn là lợi ích của việc tách dùng dependency khỏi tạo dependency.This time ZaloNotifier handles the call and sends through Zalo OA. Swapping implementations without editing order logic is the benefit of separating using a dependency from creating it.
9OrderServiceTest không khởi động Spring: tạo repository giả và FakeNotifier, tự new OrderService(repo, notifier), gọi placeOrder rồi kiểm tra FakeNotifier đã ghi đúng một lần gửi tới 0909123456 có chứa A1024 (như verify của Mockito). Không SMS thật nào được gửi.OrderServiceTest starts no Spring context: it creates a fake repository and a FakeNotifier, news up OrderService(repo, notifier), calls placeOrder, then checks the FakeNotifier recorded exactly one send to 0909123456 mentioning A1024 (like Mockito's verify). No real SMS is sent.