1

I am trying to understand the difference between composition and aggregation and what I have understood so far the key difference is in the life span of the object.

So if I have an office and an address class, is the relationship a composition or aggregation in a class diagram?

I am new to this concept. Any help appreciated

Thanks

4

2 に答える 2

0

Having an aggregation relation between two classes means, that objects of that class contain one or several objects of the other classes. Example: Department-Employee. A department contains several employees.

The composition relation is itself a kind of aggregation relation. In addition to the above it says that the lifetime of the whole and the part(s) is thightly coupled. I.e. the whole cannot exist without the part(s) and the parts cannot exist without their whole. Example: Office-Department. The office cannot exist without its departments and keeping the departments without their office doesn't make sense either. Department-Employee is not a composition relation because the employees can easily quit their jobs, retire or get run over by a bus without the company being affected. Also the company may bust and the employees can still live on.

In your example I'd say, the office-address relation is neither an aggregation nor a composition, because the one does not contain the other. It's rather a simple association (no diamond shape on either end).

于 2012-09-17T14:06:00.730 に答える
0

The key feature as defined OMG UML Superstructure Specification is

In Composition the life time of the objects is constrained by the container.

In Aggregation the life time of the objects is independent of each other.

于 2012-09-18T12:56:37.727 に答える