Module 27 ยท Design Patterns (All 23 GoF) โ Lesson 8 of 23 ยท ~6 min
Composite
Treat parts and wholes uniformly
Composite lets you build a tree where a single item and a group of items share the same interface, so client code can call total_price() on either without checking which it is. A walk package can hold plain items and sub-packages; a group's price is just the sum of its children.
The trick: both the leaf and the group answer the same method. Recursion falls out naturally โ a group asks each child for its price.