UML Notation - Aggregations/Compositions vs “Vanilla” Associations

前端 未结 5 1294
时光说笑
时光说笑 2020-12-15 12:54

I\'ve recently spent a good deal of time performing detailed UML designs of various SW components that I have since written. Looking back on what I have recently finished an

5条回答
  •  别那么骄傲
    2020-12-15 13:38

    In fact, most cases of associations in UML class models are neither aggregations nor compositions. For instance, the association between the classes Publisher and Book for assigning the books published by a publisher to this publisher is neither an aggregation nor a composition because the books published by a publisher are not parts or components of this publisher.

    A model of the association between Publisher and Book

    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 Car and Engine and between the classes Course and 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 Car and Engine is a composition (because an engine cannot be shared between two cars at the same time), while an aggregation between Course and 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.

提交回复
热议问题