Difference between Composition and Dependency in class diagram?

后端 未结 3 630
情歌与酒
情歌与酒 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:55

    These things are far from each other.

    A- - - ->B  Dependency 
    

    is the most common thing. It means, that code of A is beware of class B. And a change in B members visible to A could require a change in A.

    A------->B association (with none aggregation)
    

    Association is more close connection. The association can have different levels of closeness, but even on the weakest one it must have at least one navigability arrow. (if they are both sided, they are not shown) It means, that there exists some easy way to point from A to B. For example, there exist a construction as a.x.y.b. The association has such attribute as aggregation. It could be none, shared, composition.

    A<>------>B  association (with shared aggregation) 
    

    Shared has NO strict definition, it is left to us modelers and tools authors. But usually it shows that A instance or class in some sense has some instances of B.

    A♦------>B  association (with composite aggregation or simply 'composition') 
    

    It has strict definition - that means, that A or its instance has instances of B. It also implies, that these B's exist only in the borders of this association. When the association or its owned will be destroyed, there will be NO access to these B's. If these strict demands are not fulfilled, it is NOT a composition.

    Citation from UML standard 2.5: "Composite aggregation is a strong form of aggregation that requires a part (see 11.2.3) instance be included in at most one composite instance at a time. If a composite instance is deleted, all of its parts are normally deleted with it."

    Other variants for associations and dependencies also exist.

    So, dependency is an ancestor of the owner of composition.

提交回复
热议问题