Difference between Composition and Dependency in class diagram?

后端 未结 3 524
情歌与酒
情歌与酒 2021-01-27 06:36

I know, somebody had asked the same question about this case, but i still don\'t really get it, i need a specific answer. Thank you :D

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-27 06:41

    Since Gangnus did not correctly explain the meaning of composition, I'll have to do it.

    As explained by Gangnus, an aggregation is a special form of association with the intended meaning of a part-whole-relationship, but without a precise semantics (the UML spec says: "Precise semantics of shared aggregation varies by application area and modeler"). For instance, we can model an aggregation between the classes Carand Engine and between the classes Courseand Lecture since an engine is part of a car and a lecture is part of a course.

    A composition (also called "composite aggregation" in the UML spec) is a special form of aggregation, where a component instance is part of at most one aggregate instance at a time (that is, it cannot be shared between several aggregates). This means that the aggregation between Carand Engine is a composition (because an engine cannot be shared between two cars at the same time), while an aggregation between Courseand Lecture is not necessarily a composition since a lecture can be shared among two courses (e.g. a database management course and a software engineering course could share a lecture on UML). This implies that the multiplicity of a composition's association end at the aggregate side is either 1or 0..1, while it may be also * in the case of a non-composite aggregation.

    In addition to this main characteristics of a composition (to have exclusive parts), a composition may also come with a life-cycle dependency between an aggregate and its components implying that when an aggregate is deleted, all of its parts are deleted with it. However, this only applies to some cases of composition, and not to others, and it is therefore not a defining characteristic. The UML spec states: "A part may be removed from a composite instance before the composite instance is deleted, and thus not be deleted as part of the composite instance." In our example of the Car-Engine composition, it's clearly the case that the engine can be removed from the car before the car is destroyed, in which case the engine is not destroyed and can be re-used.

提交回复
热议问题