1LiveMapService dựng snapshot 10.000 tài xế và hỏi factory style bike cho tài xế #101. Tra theo loại xe giúp tránh nạp lại icon cho từng người.LiveMapService builds a snapshot of 10,000 drivers and requests the bike style for driver #101. Looking up by vehicle type avoids loading an icon for every driver.
2Cache chưa có bike nên factory nạp bike.png và lưu MarkerStyle. Icon và màu thuộc về loại xe, không thuộc riêng tài xế #101.The cache has no bike entry, so the factory loads bike.png and stores a MarkerStyle. Its icon and color belong to the vehicle type, not to driver #101.
3snapshot() tạo new DriverMarker(101, lat, lng, heading, style): marker chỉ giữ tọa độ, hướng và tham chiếu tới style bike vừa lấy. Bản Go Marker.Draw() in ra dòng dưới, với icon lấy từ flyweight.snapshot() creates new DriverMarker(101, lat, lng, heading, style): the marker holds only coordinates, heading and a reference to the bike style just obtained. The Go Marker.Draw() prints the line below, reading the icon from the flyweight.
4Đến tài xế #102, service lại hỏi style bike và gặp cache hit. Factory trả cùng instance nên không đọc file hay cấp phát thêm icon.For driver #102, the service requests bike again and gets a cache hit. The factory returns the same instance, avoiding another file read and icon allocation.
5Marker #101 và #102 cùng trỏ tới bike nhưng giữ vị trí riêng. Cập nhật hướng của #102 không làm #101 đổi hướng vì hướng nằm trong context.Markers #101 and #102 reference bike while keeping their own positions. Updating #102's heading leaves #101 unchanged because heading belongs to the context.
6Tài xế #103 đi ô tô nên service hỏi khóa car thay vì bike. Khóa khác giúp factory chọn đúng icon và màu, không dùng nhầm style xe máy.Driver #103 drives a car, so the service requests car instead of bike. A different key selects the correct icon and color rather than reusing the motorbike style.
7Factory nạp car.png và giữ style thứ hai để marker #103 cùng các ô tô sau dùng chung. Với hai loại xe, cache chỉ cần hai style dù số tài xế tăng lên.The factory loads car.png and retains a second style for marker #103 and subsequent cars. With two vehicle types, the cache needs only two styles as the driver count grows.
810.000 marker giờ chỉ dùng hai icon: 81.920 byte thay vì 409.600.000 byte dữ liệu icon, chưa tính marker và chi phí quản lý. Giữ style bất biến để việc sửa một marker không vô tình đổi diện mạo cả đội xe.The 10,000 markers now share two icons: 81,920 bytes instead of 409,600,000 bytes of icon data, excluding markers and overhead. Keep styles immutable so changing one marker cannot accidentally change the whole fleet's appearance.
9Bản Go: BuildMarkers(f, drivers) gọi f.Get(d.Vehicle) cho từng tài xế; StyleFactory khóa sync.Mutex rồi trả *MarkerStyle đã có. Nếu thiếu file icon của một loại xe mới, Get trả error và BuildMarkers dừng thay vì vẽ marker không có style.The Go version: BuildMarkers(f, drivers) calls f.Get(d.Vehicle) per driver; StyleFactory locks a sync.Mutex and returns the existing *MarkerStyle. If a new vehicle type has no icon file, Get returns an error and BuildMarkers stops instead of drawing a styleless marker.