8

I understand the difference between aggregation and composition but I am struggling a bit with association. My current understanding is that an association exists between classes when ‘they use each other’, for example, one object is passed to the other during a method call. See also:

http://www.codeproject.com/Articles/330447/Understanding-Association-Aggregation-and-Composit

Both objects exist independently and, in contrast to aggregation, no object is a container class of the other. Does this mean that both objects MUST have a copy of the other(s) (e.g. 1:m relationship) or how else is the association ‘stored’. Any feedback would be very much appreciated.

cs0815
  • 16,751
  • 45
  • 136
  • 299
  • I believe the biggest point of the article is to say they neither one constructs the other. SO they are not in charge of disposing of each other either. This is a very good thing in my opinion, although I think the articles example is hacky. – awright18 Oct 24 '12 at 08:02
  • Thanks for all your answers. I guess my question is more along the lines. If there is an association between two objects, do they have to have copies of each other or is it enough to express this in the signature of the methods, which establish the association implicitly? – cs0815 Oct 24 '12 at 08:07
  • 1
    Typically in C# they would just refer to each other because objects are reference types by default except for the obvious value types, so no copies in most cases. – awright18 Oct 24 '12 at 08:11

8 Answers8

12

From the UML Superstructure 2.4.1:

An association declares that there can be links between instances of the associated types. A link is a tuple with one value for each end of the association, where each value is an instance of the type of the end. (UML Superstructure, Page 37)

Nothing more, nothing less. and very vague. Because of this, it is also very hard to understand. What I defined (In a course I teach) is a hierarchy of links from dependency to composition where:

  1. Dependency from A to B means that A uses B but indirectly (say by receiving instances of it and forwarding them to other objects).
  2. Association from A to B means that A uses B directly, (for example by calling methods)
  3. Aggregation from A to B means that B is part of A (semantically) but B can be shared and if A is deleted, B is not deleted. Note that this says nothing about how the "is part" is implemented.
  4. Composition from A to B is like Aggregation, where B cannot be shared and if A is deleted, all of its aggregates (Bs) are deleted also.
vainolo
  • 6,907
  • 4
  • 24
  • 47
  • That would be a usage relationship. An association is more. As others have pointed out, it means 'A refers to B', but - in its plain form - without the whole-part semantics of Aggregation or Composition – Carsten Oct 26 '12 at 09:27
  • 3
    actually this answer is wrong, imo: an aggregation can be implemented nearly the same way as a 'normal' association (they both could or could not manifest in an actual reference) – Christian Oct 29 '12 at 07:40
  • Actually, I didn't say the you can't implement association the same way as aggregation - just that it is not a must. From the UML Superstructure: "An association describes a set of tuples whose values refer to typed instances". Does this mean that every association should be implemented as a class that stores a link to the two objects? Don't think so. In my interpretation of UML, defining aggregation (or composition) means that one class has an instance of the other. Simple association means usage. But that is only my interpreation (and the problem with UML). – vainolo Oct 29 '12 at 08:05
  • 2
    Um, sorry about the late comment, but I thought aggregation was the weaker form of association. IE - shouldn't your definitions for aggregation and composition be swapped? Composition should be the type with the solid diamond (full ownership), and aggregation should be the hollow diamond (ownership of reference); Right? – Ian Jan 02 '14 at 19:36
  • The diamonds don't matter. The definition is not exact, and you have to interpret it yourself. I invite you to read the UML documents :-) – vainolo Jan 02 '14 at 20:57
  • 1
    I hope you made a mistake here and didn't teach your students this because your descriptions of aggregation and composition should indeed be the other way around, as Ian already pointed out. – Revolutionair Nov 11 '14 at 12:25
  • @Revolutionair you are right for the correction. The definition here was incorrect and not what I taught :-). No idea how this slipped two years... – vainolo Nov 12 '14 at 13:11
  • 1
    As far as I can tell it is because you missed his point about aggregation being weak but instead dived into diamonds and invited him to read UML documents :-) – Revolutionair Nov 12 '14 at 15:37
  • The UML concept of association is not based on "uses". There is no need for the objects that participate in an association to "use each other". See http://www.codeproject.com/Articles/880451/Really-Understanding-Association-Aggregation-and-C – Gerd Wagner Mar 23 '15 at 14:16
  • 1
    here is better explanation on SO http://stackoverflow.com/questions/885937/difference-between-association-aggregation-and-composition – zish Jan 08 '16 at 16:45
3

Aggregation is an Association relationship where the Association can be considered the containing class 'Owning' the contained class, and the lifetime of that relationship is not defined.

Association is an 'Has-A' relationship.

Example:-

  public class Person  
  {  
   private final Name name;  
   private Address currentAddress;  

   //...  
 } 

In this case, the Person Has-A name and Has-A Address, so there is an Association between Person and Name, and Person and Address.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • 4
    "has-a" usually refers to an aggregation :) – Christian Oct 24 '12 at 09:11
  • Thats correct. Usually Association refers to Aggregation as Aggregation is a special case of association. A directional association between objects. When an object ‘has-a’ another object, then you have got an aggregation between them. Direction between them specified which object contains the other object. Aggregation is also called a “Has-a” relationship. – Rahul Tripathi Oct 24 '12 at 09:16
2

An association describes a relationship between instances of one or more classes. In the words of the UML Reference Manual, "Associations are the glue that holds together a system."

Aggregation is a form of association in which there is a "whole-part" relationship. You may say that if a class Airplane has a class Engine then this forms a "whole-part" relationship.

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
2

Association

Association is a relationship where all objects have their own life-cycle and there is no owner. Let’s take the example of Teacher and Student. Multiple students can associate with a single teacher and a single student can associate with multiple teachers but there is no ownership between the objects and both have their own life-cycle. Both can create and delete independently.

Aggregation

the objects in Aggregation have their own life-cycle but there is ownership. Child object can not belong to another parent object. Let’s take an example of Department and teacher. A single teacher can not belongs to multiple departments, but if we delete the department teacher object will not destroy. We can think about the “has-a” relationship.

Composition

It is a strong type of Aggregation. Child object does not have their life-cycle and if parent object deletes all child object will also be deleted. Let’s take again an example of the relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room can not belongs to two different houses if we delete the house room will automatically delete.

Santosh
  • 1,027
  • 13
  • 12
1

Aggregation

Let's set the terms. The Aggregation is a metaterm in the UML standard, and means BOTH composition and shared aggregation, simply named shared. Too often it is named incorrectly "aggregation". It is BAD, for composition is an aggregation, too. As I understand, you meant you understand "shared aggregation and composition".

From UML standard:

Precise semantics of shared aggregation varies by application area and modeler.

I haven't found a word about that aggregation supposed multiplicity, for example.

Association.

A definition from UML 3.4.1 standard:

An association describes a set of tuples whose values refer to typed instances. An instance of an association is called a link. A link is a tuple with one value for each end of the association, where each value is an instance of the type of the end.

Aggregated relationship is a subclass of Association.

Association is based on relationship. IT is the glue for models.

But your feelings didn't lie - as the shared aggregation is not strictly defined, there is also NO any strictly defined boundary between Association and Aggregated association. Authors of tools and modellers have to set it themselves.

Gangnus
  • 24,044
  • 16
  • 90
  • 149
1

Association

It represents a relationship between two or more objects where all objects have their own lifecycle and there is no owner. The name of an association specifies the nature of relationship between objects. This is represented by a solid line.

Let’s take an example of relationship between Teacher and Student. Multiple students can associate with a single teacher and a single student can associate with multiple teachers. But there is no ownership between the objects and both have their own lifecycle. Both can be created and deleted independently.

Aggregation

It is a specialized form of Association where all object have their own lifecycle but there is ownership. This represents “whole-part or a-part-of” relationship. This is represented by a hollow diamond followed by a line.

Let’s take an example of relationship between Department and Teacher. A Teacher may belongs to multiple departments. Hence Teacher is a part of multiple departments. But if we delete a Department, Teacher Object will not destroy.

Syeful Islam
  • 3,785
  • 1
  • 20
  • 19
1

It depends on the context.

Association: A man drives a car, focus on the caller and callee relationship.

Aggregation: A man has a car, focus on the owner and member relationship.

Composition: A man has a mouth, focus on the owner & member but the owner consists of members, it means that they shared the same life cycle.

Feels like I'm speaking Chinglish.

asap diablo
  • 178
  • 2
  • 13
0

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

See also my alternative CodeProject article.

Gerd Wagner
  • 5,481
  • 1
  • 22
  • 41