1Lan sắp sửa tin áo thun giá 199.000đ. Màn hình sửa (client) gọi checkpoint() trước khi sửa để có mốc quay lại nếu Lan gõ nhầm.Lan is about to edit a T-shirt listing priced at 199,000 VND. the editor client calls checkpoint() before editing to provide a recovery point for a typo.
2DraftHistory gọi ProductDraft.save() thay vì đọc từng trường. Chỉ bản nháp biết cần lưu những gì để sau này khôi phục đầy đủ.DraftHistory calls ProductDraft.save() instead of reading individual fields. The draft itself knows what must be captured for a complete restoration.
3ProductDraft tạo Snapshot chứa tên, giá 199.000đ và danh sách ảnh. Dữ liệu ảnh được giữ ổn định để lần sửa sau không làm thay đổi quá khứ.ProductDraft creates a Snapshot with the title, price of 199,000 VND, and image list. Image data stays stable so later edits cannot rewrite the past.
4save() trả bản chụp cho DraftHistory để đẩy lên stack; lịch sử không đọc giá bên trong. Giữ mốc mới nhất ở đầu giúp lần Undo tiếp theo lấy đúng mốc vừa lưu.save() returns the snapshot for DraftHistory to push onto its stack without reading the price inside. Keeping the newest checkpoint on top makes the next Undo select the right state.
5Lan gõ thiếu số 9, client gọi reprice(19000). Bản nháp đổi giá nhưng Snapshot vẫn giữ 199.000đ, nên còn đường quay lại.Lan misses a digit, and the client calls reprice(19000). The draft changes while the Snapshot keeps 199,000 VND, preserving a way back.
6Thấy sai giá, Lan bấm Hoàn tác và client gọi undo(). DraftHistory lấy mốc gần nhất khỏi stack, không cần biết những trường nào vừa thay đổi.Lan spots the wrong price, clicks Undo, and the client calls undo(). DraftHistory pops the latest checkpoint without needing to know which fields changed.
7DraftHistory chuyển bản chụp cho ProductDraft.restore(). Bản nháp tự khôi phục tên, giá và ảnh; giá trở về 199.000đ mà lớp lịch sử chưa từng mở nội dung bản chụp.DraftHistory passes the snapshot to ProductDraft.restore(). The draft restores its title, price, and images; the price returns to 199,000 VND without history ever inspecting the snapshot.
8Nếu checkpoint quá 20 lần, DraftHistory removeLast() bỏ mốc cũ nhất để giới hạn bộ nhớ; undo() khi stack rỗng trả false. Bản Go giống hệt: editor.History.Checkpoint append h.draft.Save() (ảnh được slices.Clone), Undo cắt phần tử cuối rồi gọi Restore.After more than 20 checkpoints, DraftHistory calls removeLast() to drop the oldest and bound memory; undo() on an empty stack returns false. Go mirrors this: editor.History.Checkpoint appends h.draft.Save() (images copied with slices.Clone), and Undo slices off the last element and calls Restore.
9History nằm ở package editor, khác package catalog, nên chỉ cất được Snapshot mà không đọc được title hay price: dòng last.price = 0 sẽ báo lỗi biên dịch. Java đạt điều tương tự nhờ Snapshot là lớp lồng có field private.History lives in package editor, not catalog, so it can store a Snapshot but cannot read title or price: last.price = 0 is a compile error. Java achieves the same with Snapshot as a nested class with private fields.