1CartService gọi price() trên thùng quà như trên bất kỳ CartItem nào. Nhờ hợp đồng chung, màn hình tính tiền không cần biết đây là món lẻ hay combo.CartService calls price() on the gift box as it would on any CartItem. The shared contract means checkout does not need to know whether this is a product or a bundle.
2Thùng quà gọi price() trên hộp đồ uống trước. Vì con cũng là Bundle, nó tự tính phần bên trong thay vì bắt thùng quà duyệt hộ.The gift box first calls price() on the drinks box. Since this child is also a Bundle, it prices its own contents rather than making the parent traverse them.
3Hộp đồ uống gọi price() trên lốc Coca. Đây là lá nên trả ngay 60.000 đồng, không cần mở thêm tầng nào.The drinks box calls price() on the Coca pack. This is a leaf, so it immediately returns 60,000 VND without descending further.
4Cùng lời gọi price() đó được gửi tới thùng bia, trả về 280.000 đồng. Bundle chỉ cần giá của con, không cần biết loại sản phẩm cụ thể.The same price() call goes to the beer case and returns 280,000 VND. The Bundle needs each child's price, not its specific product type.
5Hộp đồ uống cộng hai giá và không giảm thêm vì mức giảm là 0%. Nó trả 340.000 đồng lên trên, gói toàn bộ chi tiết của nhánh thành một con số.The drinks box adds both prices and applies its 0% discount. It returns 340,000 VND upward, reducing all the details of this branch to one number.
6Thùng quà hỏi tiếp bánh chưng và nhận 150.000 đồng. Lá và hộp con đều cung cấp giá qua cùng một thao tác, nên vòng lặp của Bundle không cần kiểm tra kiểu.The gift box next asks the banh chung and gets 150,000 VND. Both a leaf and a nested box supply a price through the same operation, so the Bundle loop needs no type checks.
7Thùng quà cộng 340.000 với 150.000 rồi áp dụng mức giảm 10% của chính nó. Giảm giá ở cấp này tính trên giá các con đã trả về, kể cả mức giảm riêng của hộp con nếu có.The gift box adds 340,000 and 150,000, then applies its own 10% discount. This level's discount uses the prices returned by its children, including any discounts they already applied.
8CartService nhận 441.000 đồng để đưa vào tổng giỏ hàng. Thêm tầng combo vẫn giữ nguyên cách gọi này, vì mỗi nhánh chịu trách nhiệm tính phần cây của mình.CartService receives 441,000 VND to include in the cart total. More bundle levels leave this call unchanged because each branch prices its own subtree.
9Khi thật sự cần phân biệt lá và nhánh, countProducts() dùng switch trên sealed interface: Product đếm 1, Bundle đệ quy qua children, ra 3 sản phẩm. Bản Go có printTree() dùng type assertion it.(*cart.Bundle) để in cây có thụt lề.When leaves and branches really must be told apart, countProducts() switches over the sealed interface: a Product counts 1, a Bundle recurses through children, giving 3 products. The Go version has printTree(), which uses the type assertion it.(*cart.Bundle) to print an indented tree.