1CampaignRegistry dựng sẵn hai bản mẫu: FLASH_SALE (electronics, fashion, GOLD 20%) và MEGA_SALE (all, GOLD 30%). Từ đây bản mẫu chỉ được đọc để sao chép.CampaignRegistry pre-builds two templates: FLASH_SALE (electronics, fashion, GOLD 20%) and MEGA_SALE (all, GOLD 30%). From then on, templates are read only for copying.
2Linh cần đợt 11.11, nên createDoubleDay(11) gọi registry.newFrom("MEGA_SALE") thay vì nhập lại từng thiết lập. Khóa này chọn MEGA_SALE chứ không phải FLASH_SALE.Linh needs the 11.11 sale, so createDoubleDay(11) calls registry.newFrom("MEGA_SALE") instead of rebuilding every setting. The key selects MEGA_SALE rather than FLASH_SALE.
3newFrom() tìm thấy mẫu và trả về proto.copy(), không bao giờ trả bản gốc. copy() gọi copy constructor, tạo ArrayList và HashMap mới nên bản sao có danh mục và ưu đãi riêng.newFrom() finds the template and returns proto.copy(), never the master itself. copy() calls the copy constructor, which creates a new ArrayList and HashMap so the copy owns its categories and discounts.
4Service gọi rename("11.11 Mega Sale"), startOn(ngày 11/11) rồi discount("GOLD", 40) trên bản sao. Các thiết lập còn lại được giữ từ mẫu nên không bị bỏ sót.The service calls rename("11.11 Mega Sale"), startOn(November 11) and discount("GOLD", 40) on the copy. Other settings carry over from the template, avoiding omissions.
5Đối chiếu: bản sao có GOLD 40% còn MEGA_SALE vẫn 30%, chứng tỏ map đã tách riêng. Lần sau createDoubleDay(12) lại nhận bộ mặc định ban đầu.Compare: the copy has GOLD at 40% while MEGA_SALE still has 30%, showing the maps are independent. Next time createDoubleDay(12) gets the original defaults again.
6Khóa nào không có trong registry thì newFrom() ném lỗi ngay, thay vì trả về một Campaign rỗng. FLASH_SALE cũng được sao chép theo đúng cách đó khi được gọi tới.A key missing from the registry makes newFrom() throw immediately instead of returning an empty Campaign. FLASH_SALE is copied the same way whenever it is requested.
7Bản Go mở đầu bằng cái bẫy: shallow := *master chép các field nhưng map Discounts vẫn dùng chung. Sửa GOLD thành 50 trên shallow làm master cũng thành 50, nên main phải khôi phục về 30.The Go version opens with the trap: shallow := *master copies the fields but the Discounts map is still shared. Setting GOLD to 50 on shallow makes master 50 too, so main has to restore it to 30.
8main đăng ký master với khóa MEGA_SALE rồi gọi reg.New("MEGA_SALE"). Registry trả về p.Clone(): slices.Clone và maps.Clone cấp vùng nhớ riêng, sau đó main đổi tên, ngày và GOLD = 40 trên sale.main registers master under MEGA_SALE and calls reg.New("MEGA_SALE"). The registry returns p.Clone(): slices.Clone and maps.Clone give separate storage, then main changes the name, date and GOLD = 40 on sale.
9Dòng in cuối xác nhận bản sao sâu giữ giá trị riêng. Khác biệt duy nhất giữa hai lần chạy Go là shallow chép header của map, còn Clone() chép cả dữ liệu.The last print confirms the deep copy keeps its own value. The only difference between the two Go runs is that shallow copies the map header, while Clone() copies the data too.