1D7 và D9 gọi goOnline(); mỗi Driver chỉ biết Dispatcher nên gọi dispatcher.driverAvailable(this). DispatchCenter thêm cả hai vào tập idle — hai tài xế không cần hỏi nhau ai nhận khách tiếp theo.D7 and D9 call goOnline(); each Driver knows only the Dispatcher, so it calls dispatcher.driverAvailable(this). DispatchCenter adds both to the idle set — the drivers never ask each other who takes the next rider.
2Mai (số 0909) gửi điểm đón chợ Bến Thành qua rideRequested(rider, pickup). Phía khách không giữ danh sách tài xế; method synchronized khiến hai yêu cầu cùng lúc được xử lý lần lượt.Mai (phone 0909) submits her Ben Thanh pickup via rideRequested(rider, pickup). The rider side keeps no driver list; the synchronized method processes simultaneous requests one at a time.
3Trung tâm chạy idle.stream().min(comparingDouble(d -> d.location().distanceTo(pickup))) và chọn D7, tài xế gần hơn. Luật chọn nằm ở đây nên đổi tiêu chí không phải sửa Rider hay Driver.The center runs idle.stream().min(comparingDouble(d -> d.location().distanceTo(pickup))) and picks D7, the nearer driver. The rule lives here, so changing criteria never touches Rider or Driver.
4ifPresentOrElse: trung tâm idle.remove(D7) rồi gọi driver.assign(rider); Driver in thông báo nhận khách.ifPresentOrElse: the center calls idle.remove(D7), then driver.assign(rider); the Driver prints its pickup notice.
5Tiếp theo trung tâm gọi notifier.sms(rider.phone(), ...) báo cho Mai. D7 không cần biết dịch vụ gửi tin. Yêu cầu sau sẽ nhận D9; khi idle rỗng, nhánh orElse gửi SMS xin thử lại.Next the center calls notifier.sms(rider.phone(), ...) to tell Mai. D7 needs no knowledge of messaging. The next request gets D9; once idle is empty, the orElse branch sends a retry SMS.
6Bản Go: main gọi dispatch.New() rồi go d.Run(ctx). GoOnline(Driver{ID: "D7"}) gửi tài xế vào channel online; goroutine Run là nơi duy nhất sở hữu map idle nên không cần mutex.In Go: main calls dispatch.New() then go d.Run(ctx). GoOnline(Driver{ID: "D7"}) sends the driver on the online channel; the Run goroutine alone owns the idle map, so no mutex is needed.
7main tạo reply := make(chan string, 1) và gọi Request(RideRequest{Phone: "0909", Pickup, Reply: reply}). Yêu cầu đi qua channel requests tới Run.main creates reply := make(chan string, 1) and calls Request(RideRequest{Phone: "0909", Pickup, Reply: reply}). The request travels through the requests channel to Run.
8Run gọi nearest(idle, req.Pickup); nearest so Loc.DistanceTo(p) và trả (D7, true). Run delete(idle, "D7") để không giao trùng.Run calls nearest(idle, req.Pickup); nearest compares Loc.DistanceTo(p) and returns (D7, true). Run calls delete(idle, "D7") so D7 is not assigned twice.
9Run gửi kết quả vào req.Reply thay cho bước SMS; main nhận <-reply và in ra. Nếu không có tài xế, Reply nhận "no drivers nearby, please retry".Run sends the result to req.Reply instead of an SMS; main receives <-reply and prints it. With no driver available, Reply gets "no drivers nearby, please retry".