Object-oriented – UML open diamond help

object-orienteduml

enter image description here

Lets say A, B are classes then what does this UML diagram mean?

(1) A contains B

Or

(2) B contains A

I'm confused as to which direction the aggregation holds.

Best Answer

In both cases the class closest to the diamond is the one that contains the class farthest from it. The relationship indicates either a single contained object or a collection of them. Usually it is indicated graphically with a legend similar to the one used in database ERM diagrams: "1..1", "1..m", etc.

The filled diamond means composition. The contained objects has little or no use outside the container and perhaps the container is incomplete without them (or is in an invalid/unstable state without them). For example, every iPhone has a touch screen panel (the transparent thing with a flat cable attached to the side). The touch screen panel separated from the iPhone is not of much use. You don't see people carrying in their pockets detached touch screen panels with the flat cable hanging out.

The empty diamond means aggregation. The contained object can exist separatelly from the container and is useful outside the container. The container can live without the contained object. A keychain is a composite object with a lot of keys attached to it, the keys, laser pointers and can openers can work by themselves without being attached to a keychain and neither the keychain or the tool (key, etc) is left in an unstable state. You can add and remove this items to the keychain but they can exist by themselves as functional tools. The keychain itself can be epmty, and you can't say it is broken because of that.

See also this answer: https://softwareengineering.stackexchange.com/a/336764/61852