1Lúc biên dịch, Lombok sinh Shipment.ShipmentBuilder cùng builder() và toBuilder(), giúp Mai khỏi viết phần gán giá trị lặp lại. Quy tắc kiểm tra vẫn nằm trong compact constructor của record Shipment.At compile time, Lombok generates Shipment.ShipmentBuilder, builder(), and toBuilder(), saving Mai repetitive assignment code. Validation rules stay in the Shipment record's compact constructor.
2Constructor của ShippingService nhận RestClient.Builder do Spring Boot cấu hình sẵn, rồi gọi baseUrl(), defaultHeader("Token", ...) và build() để có RestClient ghn. Đây là builder có sẵn trong framework.ShippingService's constructor receives the RestClient.Builder that Spring Boot pre-configures, then calls baseUrl(), defaultHeader("Token", ...) and build() to get the ghn RestClient. This is a builder the framework already provides.
3ship("DH1001") mở Shipment.builder() và gọi các bước có tên: orderId, receiverPhone, address, item("iPhone 16"), item("Silicone case") nhờ @Singular, rồi fragile(true). Không đặt codAmount vì đơn đã thanh toán.ship("DH1001") opens Shipment.builder() and calls named steps: orderId, receiverPhone, address, item("iPhone 16"), item("Silicone case") thanks to @Singular, then fragile(true). codAmount is left unset because the order is prepaid.
4build() gọi compact constructor: orderId và address có đủ nên qua kiểm tra, codAmount null được gán BigDecimal.ZERO. Sau đó ship() gửi Shipment tới /shipping-order/create qua ghn.post().build() calls the compact constructor: orderId and address are present so validation passes, and the null codAmount becomes BigDecimal.ZERO. ship() then posts the Shipment to /shipping-order/create via ghn.post().
5Nếu một lần gọi quên address rồi build(), chính constructor đó từ chối. Nhờ kiểm tra tại chỗ tạo kết quả, đơn thiếu địa chỉ không thể đi tới hãng vận chuyển.If a call forgets address and then calls build(), that same constructor rejects it. Checking at construction keeps an address-less shipment from reaching the carrier.
6Khách đổi điểm nhận sang 34 Lê Lợi: shipment.toBuilder() trả về builder mới chứa toàn bộ giá trị cũ, Mai chỉ đặt address mới rồi build() lại. Shipment ban đầu không bị sửa vì record bất biến.The customer changes the drop-off to 34 Le Loi: shipment.toBuilder() returns a new builder holding all old values, Mai sets only the new address and calls build() again. The original Shipment is untouched because the record is immutable.
7RetryPolicy là bản viết tay của đúng thứ Lombok sinh ra: constructor private nhận Builder, mặc định maxAttempts = 3 nằm trong RetryPolicy.Builder, và build() là cổng kiểm tra duy nhất.RetryPolicy is the hand-written version of what Lombok generates: a private constructor taking the Builder, the maxAttempts = 3 default inside RetryPolicy.Builder, and build() as the single validation gate.
8Bản Go không có builder class: main gọi ghn.New(url, token, ghn.WithTimeout(2*time.Second), ghn.WithRetries(5)). New đặt mặc định trước (timeout 5s, retries 3) rồi chạy từng Option để ghi đè.The Go version has no builder class: main calls ghn.New(url, token, ghn.WithTimeout(2*time.Second), ghn.WithRetries(5)). New sets defaults first (5s timeout, 3 retries), then runs each Option to override them.
9New tạo http.Client nếu chưa có rồi trả về *Client đã cấu hình. main in Shipment dựng bằng literal có tên field và thời gian chờ của client.New creates an http.Client if none was given and returns the configured *Client. main prints the Shipment built with a named-field literal and the client's timeout.