60

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 me.

I know dependency "uses-a" and inheritance "is-a" but I'm a bit unfamiliar with Aggregation, Composition, Association and Direct Association; also, which of them is "has-a" relationship. Some use Aggregation interchangeably with Association.

What is Direct Association? Also, what is Composition? In UML diagrams, the arrows that represents them are different. I would be really thankful if you could clear these things out for me.

rook
  • 5,880
  • 4
  • 39
  • 51
steven0529
  • 1,513
  • 1
  • 18
  • 28

4 Answers4

136

Please note that there are different interpretations of the "association" definitions. My views below are heavily based on what you would read in Oracle Certification books and study guides.

Temporary association

A usage inside a method, its signature or as a return value. It's not really a reference to a specific object.

Example: I park my Car in a Garage.

Temporary Association UML

Composition association

A so-called "STRONG relationship": The instantiation of the linked object is often hard coded inside the constructor of the object. It cannot be set from outside the object. (Composition cannot be a many-to-many relationship.)

Example: A House is composed of Stones.

Composition UML

Direct association

This is a "WEAK relationships". The objects can live independent and there are usually setters or other ways to inject the dependent objects.

Example: A Car can have Passengers.

Direct Association UML

Aggregation association

Very similar to a Direct association. It's also a "WEAK relationship" with independent objects. However here the associated objects are a crucial part of the containing object.

Example: A Car should have Tires.

Aggregation UML

Note: Both Direct associations and Aggregation associations are often generalized as "Associations". The difference is rather subtle.

bvdb
  • 22,839
  • 10
  • 110
  • 123
  • The name for "Composition association" is Composite Agregation. And what you call "Aggregation association" is a Shared Compostion and per UML2.5 has no defined semantics. Please see the box on. p 110. – qwerty_so Mar 30 '19 at 08:26
  • @qwerty_so to which specific book were you refering ? – bvdb Jan 05 '21 at 07:47
  • UML 2.5 by OMG: https://www.omg.org/spec/UML/2.5/About-UML/ – qwerty_so Jan 05 '21 at 09:12
16

The whole point of OOP is that your code replicates real world objects, making your code readable and maintainable.

1. Association

Association is: Class A uses Class B.

Example:

  • Employee uses Bus/train Services for transportation.
  • Computer uses keyboard as input device

And in In UML diagram Association is denoted by a normal arrow head.

2. Aggregation

Class A contains Class B, or Class A has an instance of Class B.

An aggregation is used when life of object is independent of container object. But still container object owns the aggregated object.

So if we delete class A that doesn't mean that class B will also be deleted. E.g. none, or many, teachers can belong to one or many departments.

The relationship between Teachers and Departments is aggregation.

3. Composition

Class A owns Class B.

E.g. Body consists of Arm, Head, Legs. BankAccount consists of Balance and TransactionHistory.

So if class A gets deleted then also class B will get deleted.

user 451
  • 462
  • 6
  • 8
6

Direct association has nothing in common with the other three. It does not belong to UML at all, it is the IBM requirements modelling term.

As for others,

Association A->B is a child of Dependency. Association means, that A (or its instance) has some easy way to get to instance of B. For example, a.x.y.b. Or by function, or by some local variable. Or by a direct reference or pointer, or something else (there are many languages in the world). As you see, there is no strict border between dependency and association.

One of attributes of Association is Aggregation, it can have values: None, shared (often incorrectly called aggregation), and composition.

If A (or instance) has some (or one) instances of B so, that destroying of association means the destroying of B instances, it is the composition.

If you or a tool author had decided, that some has-a relationship, that is weaker that composition, needs to be specially shown, you can use shared aggregation. Usually it is some collections of references to B in A.

There are some more interesting attributes of associations. Look here if you are interested.

Community
  • 1
  • 1
Gangnus
  • 24,044
  • 16
  • 90
  • 149
  • Please notice that in UML, the concept of Association is not derived from that of Dependency. An Association is a Relationship and a Classifier, while a Dependency is a DirectedRelationship. Also your explanation of composition is incorrect, see my answer to [this question](http://stackoverflow.com/questions/21951602/difference-between-composition-and-dependency-in-class-diagram). – Gerd Wagner Feb 23 '14 at 22:32
  • It is not derived as a model element. But by their definitions, every association A--->B IS also a dependency A- - >B. One is a subset of another. Thank you, alas, I have noticed, that they don't name it so. – Gangnus Feb 23 '14 at 22:39
  • No that statement is plain false. In that case every association would have to be a directed relationship, which is not true. – Gerd Wagner Feb 23 '14 at 22:40
  • I am terribly sorry, could you explain what you mean? – Gangnus Feb 23 '14 at 22:41
  • Dependency - a relationship where change in one affect the other. That works for association, too. – Gangnus Feb 23 '14 at 22:43
  • Just look it up in the UML 2.5 spec, which defines Dependency on p. 28 as a directed relationship. – Gerd Wagner Feb 23 '14 at 22:50
  • @gwag sorry, I'll check it at work tomorrow. Thank you anyway for your keeping me in form :-) – Gangnus Feb 23 '14 at 22:55
  • @gwag "A Dependency signifies a supplier/client relationship between model elements where the modification of a supplier may impact the client model elements." That fits for any association, too. That fact that in model association is not derived from dependency, is irrelevant. We are not talking about UML model construction, but about the USE of elements. – Gangnus Feb 24 '14 at 09:54
  • So, you want to override the UML metamodel as irrelevant, because your private interpretation counts more? – Gerd Wagner Feb 24 '14 at 12:10
  • @gwag You have very serious problems with understanding how the classes generalization is different from mathematical subsets. Association is not a subclass of Dependency, but Associations are the subset of Dependencies. And that fact breaks nothing in the UML model. – Gangnus Feb 24 '14 at 12:34
4

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.

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