Aggregation vs Composition vs Association vs Direct Association

后端 未结 4 2052
遥遥无期
遥遥无期 2020-12-07 07:46

I am reviewing my knowledge in object-oriented programming. Under the relationship between classes topic, I have encountered some relationships which are a bit ambiguous to

4条回答
  •  悲&欢浪女
    2020-12-07 08:15

    An association between object types classifies relationships between objects of those types. For instance, the association Person-isEmployedBy-Enterprise may classify the relationships PeterMiller-isEmployedBy-IBM, SusanSmith-isEmployedBy-IBM and SarahAnderson-isEmployedBy-Google between the objects PeterMiller, SusanSmith and SarahAnderson of type Person as well as Google and IBM of type Enterprise. In other words, associations are relationship types with two or more object types participating in them. An association between two object types is called binary. While binary associations are more common, we may also have to deal with n-ary associations, where n is a natural number greater than 2. For instance, Person-isTreatedIn-Hospital-for-Disease is a 3-ary ("ternary") association between the object types Person, Hospital and Disease.

    I guess that with "direct association" you mean a directional (or directed) association, which is an association (with a domain class and a range class) that represents a reference property in its domain class. Such a directional association has an "ownership dot" at its target end.

    Please see this book chapter for more about associations.

    And see my answer to this SO question for an explanation of aggregations and compositions.

提交回复
热议问题