Composite pattern 00:00:00 Make a prediction You will price a tree using one operation for leaves and groups and state the containment rule that makes recursion terminate. A gift box holds a ten-dollar book and another box containing two five-dollar items. What should price return for the outer box? Twenty dollars. Each leaf returns its price, the inner box returns ten, and the outer box adds its direct children's answers. It does not need to ask which child is itself a box. 00:00:31 Noor builds a mobile A hanging mobile has simple ornaments and branches holding smaller ornaments. Lift one branch and it carries its whole subtree. You can tie a new sub-branch wherever a branch can hold another piece. Leaves and composites share the operation the client needs. Only composites need child management, and no branch can hang from its own descendant. Composite is a structural design pattern. It arranges objects in a tree of parts and wholes and gives both the same type, the component. Without it, each client walks the tree by hand and asks at every node what kind it is. The loops are copied, and a new kind must be added to all of them. 00:01:12 Price a basket of nested boxes A gift box holds a book priced at ten dollars and an inner box containing two five-dollar items. A packing slip should total the whole structure. Leaves return their own price. A box implements the same pricing operation by asking each child and summing the answers. The inner box returns ten and the outer box returns twenty. Prevent containment cycles, or a box placed inside itself would make the total recurse forever. Price the ten-dollar book and the inner box independently. Its two five-dollar items sum to ten. The outer box asks its direct children for their prices, adding ten and ten to return twenty. Remove one five-dollar item. The new fifteen-dollar total should appear through recursion without teaching the outer box about individual products. 00:02:04 Draw a group of shapes A single circle implements draw. A group also implements draw and asks each contained shape or subgroup to draw. The canvas works with either through the same contract. Child-management operations can stay on groups so callers cannot accidentally add a child to a circle. Draw the root group and count three individual draw events. Now move one shape into both child groups. A shared child might render twice, depending on your graph contract. If strict ownership is required, reject a second parent and prevent inserting a group into itself. Allowing a box to contain itself makes the pricing algorithm recur forever. Enforce a tree or explicitly define behaviour for shared nodes and cycles. If collection behaviour is genuinely unlike item behaviour, a plain collection of items can be clearer than forcing a uniform abstraction. DOM nodes. Elements, texts and the document itself are all Node. textContent of an element is the joined text of its descendants. The child methods are on Node, so calling appendChild on a text node throws: the transparent form. Java AWT and Swing. Container is a Component that can contain other AWT components. add exists on Container only: the safe form. 00:03:31 Try an interview follow-up How would you add an XML export for all kinds? With one export and a small, changing set of shapes, an export method per class may be enough. If the shape types are stable and formats keep arriving, I would put each format in a visitor. Then a new export is a new operation while its handling of each element stays together. A shipping carton can contain items and smaller cartons. Give both a weight operation and trace a carton containing two items plus an empty carton. Decide whether empty packaging has its own weight. If clients can put a carton inside itself, enforce a tree constraint before recursive measurement. 00:04:16 What to take with you One type for a part and the whole. The leaf answers, the composite asks each child and combines. The client holds one reference and makes one call. Keep the common interface small: only what makes sense for a leaf. Put add and remove on the composite unless you need every node to look the same. Decide what an empty composite answers.