I am learning how to draw a UML class diagram and after reading a few tutorials, I am still confused about the appropriate usages of Association type (The clear diamond and black diamond). I understand their usages but I haven't got a very concrete understanding on when to use them and when not to.
For example in the sample class diagram below, I would have thought that if Customer does not exist, the Order will be gone too but an association diamond was not being used.
Hope someone can provide me with more explaination/examples on the usages of association types.
Thanks.
Best Answer
An association links one class to another to indicate a relation between them. For example, you have an association between
Customer
andOrder
which (with no further information) indicates that:A customer can have an order or maybe several orders,
An order can belong to a customer or maybe shared by several customers.
The diagram in your question gives an additional info: there is one and only one customer by order, and zero or more orders by customer, so the rules become:
A customer have zero or more orders,
An order belongs to one and one only customer.
While aggregation is used in the association between
Order
andOrderDetail
, it's not used in the association betweenCustomer
andOrder
. This is done to indicate the life cycle dependency. If an instance of theOrder
is destroyed, all instances ofOrderDetail
inside it would be destroyed too; on the other hand, the instances ofOrder
are independent of the life cycle of theCustomer
instance, thus no aggregation here.This doesn't mean that an order can exist without a customer: the 1 ↔ 0..* association explicitly forbids the orders which have no customers. The difference between a 1 ↔ 0..* association and the aggregation really happens on life cycle level.