Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UML - association or aggregation (simple code snippets)

I drives me crazy how many books contradicts themselves.

Class A {} class B {void UseA(A a)} //some say this is an association,
no reference is held but communication is possible
Class A {} class B {A a;} //some say this is
    aggregration, a reference is held

But many say that holding a reference is still just an association and for aggregation they use a list - IMHO this is the same, it it still a reference.

I am very confused, I would like to understand the problem.

E.g. here: http://aviadezra.blogspot.cz/2009/05/uml-association-aggregation-composition.html - what is the difference between Strong Association and Aggregation, in both cases the author uses a field to store the reference..

Another example: This is said to be Association:

enter image description here

And this is said to be Aggregration:

enter image description here

public class Professor {
  // ...
}

public class Department {
  private List<Professor> professorList;
  // ..

}

Again, what is the difference? It is a reference in both cases

like image 514
John V Avatar asked Feb 28 '15 00:02

John V


1 Answers

This question has been, and will be, asked many times in many different variants, because many people, including many high-profile developers, are confused about the meaning of these terms, which have been defined in the UML. Since the question has been asked many times, it has also been answered many times. See, e.g. this answer. I'll try to summarize the UML definitions.

An association between two classes is not established via a method parameter, but rather via reference properties (class attributes), the range/type of which are the associated classes. If the type of a method parameter is a class, this does not establish an association, but a dependency relationship.

It's essential to understand the logical concept of associations first, before looking at how they are coded. An association between object types classifies relationships between objects of those types. For instance, the association Committee-has-ClubMember-as-chair, which is visualized as a connection line in the class diagram shown below, may classify the relationships FinanceCommittee-has-PeterMiller-as-chair, RecruitmentCommittee-has-SusanSmith-as-chair and AdvisoryCommittee-has-SarahAnderson-as-chair, where the objects PeterMiller, SusanSmith and SarahAnderson are of type ClubMember, and the objects FinanceCommittee, RecruitmentCommittee and AdvisoryCommittee are of type Committee.

The association Committee-has-ClubMember-as-chair

An association is always encoded by means of reference properties, the range/type of which is the associated class. For instance, like so

class Committee { ClubMember chair; String name;}

In the UML, aggregation and composition are defined as special forms of associations with the intended meaning of classifying part-whole-relationships. In the case of aggregation, as opposed to composition, the parts of a whole can be shared with other wholes. This is illustrated in the following example of an aggregation, where a course can belong to many degree programs.

An example of aggregation

The defining characteristic of a composition is to have exclusive (or non-shareable) parts. A composition may come with a life-cycle dependency between the whole and its parts implying that when a whole is destroyed, all of its parts are destroyed with it. However, this only applies to some cases of composition, and not to others, and it is therefore not a defining characteristic. An example of a composition where the parts (components) can be detached from the whole (composite) and therefore survive its destruction, is the following:

enter image description here

like image 132
Gerd Wagner Avatar answered Nov 04 '22 19:11

Gerd Wagner