Card 06/ 08
Aggregation vs Composition Drawn Backwards
The diamond sits on the whole, not the part, and a hollow one means the part can outlive it.
The diamond in a UML aggregation or composition goes on the end of the line next to the whole, and the most common mistake is to put it next to the part. Developers expect a mark to sit on the thing being added, the way an arrowhead points at its target, so a diagram of a car and its engine ends up with the diamond beside Engine. This card shows the mistake, the correct drawing, and the two questions that settle any diamond.
A car that owns its engine
Read as written, each engine is made of a car, and the car goes when the engine does.
The car is the whole. It creates its engine, and the engine goes when the car does.
A diamond is read from its own end: the class beside it "has" the class at the far end. That is the opposite of an arrowhead, which marks the target, and the mismatch is why the mistake is so common.
Two questions settle any diamond. First, which class is the whole? The diamond goes beside it. Second, can the part exist without the whole, or be shared between wholes? If it can, as a player outlives a team, the diamond is hollow. If the whole creates the part and nothing else holds it, as an order creates its lines, the diamond is filled.
The second question has a test in code. Look at where the part is created. If the whole's constructor or methods create it, the relationship is composition. If it arrives as a parameter from outside, it is aggregation, because something else made it and may still hold it.
← → move through sections and cards