Comparison
Aggregation vs compositionvsComposite pattern
Aggregation vs composition
you deleted the order and the line items went with it, but the customer stayed, because only one of them belonged to it.
Two shades of has-a: composition means the part's lifetime is owned by the whole and it cannot be shared, aggregation means the whole merely holds a reference to something that outlives it. The distinction is drawn as a filled or hollow diamond in UML and is worth exactly as much as the lifetime consequence — what gets deleted, what gets copied, what can be handed to two owners at once. It becomes concrete rather than academic the moment you write a delete or a deep copy.
Full entry →Composite pattern
a folder and a file answer the same size method, so the code that totals them never checks which it has.
Treating a group of objects and a single object through the same interface, so a tree can be handled without the caller distinguishing leaves from branches. Files and folders, UI elements and containers, and nested validation rules are the everyday instances. Its awkward corner is operations that only make sense on one kind — adding a child to a leaf — which forces either an interface that lies or a caller that checks after all.
Full entry →