1Đơn #A1 của shop Mai được tạo với status = OrderStatus.PENDING (Go: New("A1") gán state Pending). Mọi thao tác đều đi qua Order, không ai gán status trực tiếp.Mai's order #A1 is created with status = OrderStatus.PENDING (Go: New("A1") sets state Pending). Every action goes through Order; nobody assigns the status directly.
2Yêu cầu ship quá sớm: order.ship() chạy status = status.ship(this) trên PENDING. PENDING không override ship nên dùng mặc định throw illegal("ship"); phép gán không xảy ra, status giữ PENDING.A premature ship request: order.ship() runs status = status.ship(this) on PENDING. PENDING does not override ship, so the default throw illegal("ship") fires; the assignment never happens and status stays PENDING.
3Có thanh toán: order.pay() uỷ quyền cho PENDING.pay(o), trạng thái này trả PAID và Order đổi status. Bên gọi chỉ nêu hành động, không tự chọn PAID.Payment arrives: order.pay() delegates to PENDING.pay(o), which returns PAID, and Order swaps its status. The caller names an action and never picks PAID itself.
4Kho gọi OrderService.ship(orderId): service findById trong @Transactional rồi gọi order.ship(). Vẫn là Order đó, nhưng status mới đổi luật xử lý.The warehouse calls OrderService.ship(orderId): inside @Transactional the service runs findById, then order.ship(). Same Order, but its new status changes the rules.
5PAID.ship(o) trả SHIPPED; Order gán status mới và dirty checking của JPA tự lưu. ship(orderId) trả về SHIPPED cho bên gọi.PAID.ship(o) returns SHIPPED; Order assigns the new status and JPA dirty checking persists it. ship(orderId) returns SHIPPED to the caller.
6Khách xin huỷ sau khi hàng đã đi: order.cancel() chuyển cho SHIPPED, vốn không override cancel nên ném lỗi mặc định. Đơn vẫn SHIPPED và không có hoàn tiền.The customer asks to cancel after dispatch: order.cancel() goes to SHIPPED, which does not override cancel, so the default throws. The order stays SHIPPED with no refund.
7Shipper giao xong: SHIPPED.deliver(o) trả DELIVERED. DELIVERED không override gì, nên từ đây mọi hành động đều bị từ chối — trạng thái cuối.The courier delivers: SHIPPED.deliver(o) returns DELIVERED. DELIVERED overrides nothing, so every further action is rejected — a final state.
8Nhánh khác với đơn B2 đã trả tiền: cancel() trên PAID gọi o.refund() trước rồi trả CANCELLED. Nếu B2 còn PENDING, cancel trả CANCELLED luôn mà không hoàn tiền.Alternative path for paid order B2: cancel() on PAID calls o.refund() first, then returns CANCELLED. Had B2 still been PENDING, cancel would return CANCELLED with no refund.
9Bản Go cùng luồng: base{name} trả lỗi cho mọi hành động; pending, paid, shipped nhúng base và chỉ override hành động hợp lệ; Delivered, Cancelled là base thuần. Order.apply(next, err) chỉ đổi state khi err == nil và log chuyển trạng thái.Go follows the same flow: base{name} returns an error for every action; pending, paid and shipped embed base and override only the legal actions; Delivered and Cancelled are plain base. Order.apply(next, err) swaps state only when err == nil and logs the transition.