33

I have reviewed a lot of information about these things, but can't understand what is the difference between them? In Fowler's UML Distilled says that Aggreagation is strictly meaningless, so author recommends not to use it in diagrams. Explain, please, when I should use each of them and how it will influence on java code.

maks
  • 5,911
  • 17
  • 79
  • 123
  • You probably shouldn't have this tagged with OOP and UML. These terms mean something different in OOP than they do in UML. – keithwill Nov 28 '10 at 18:19
  • 10
    No, UML is a graphical object-oriented design notation, so they should mean the same thing. – duffymo Nov 28 '10 at 18:20
  • 1
    UML is a lot more than a graphical notation. (Though, the graphical notation does seem to be the only useful part) – Goran Jovic Nov 28 '10 at 18:26
  • This article [`Difference between Association, Aggregation and Composition`](http://guruzon.com/1/oop-concepts/inheritance-and-composition/aggregation-vs-composition-vs-association-difference-comparison-between) may be useful. – Iman Mahmoudinasab Dec 21 '13 at 07:10

6 Answers6

165

There are four kinds of Class relationships

  1. Association: uses a
    Ex:a Class Man uses a Class Pen ( Pen is still there when man die )
  2. Aggregation: has a
    Ex:a Class Man has a Class Car ( Car is still there when Man die )
  3. Composition: owns a
    Ex:a Class Man owns a Class Heart ( When Man die, Heart die )
  4. Inheritance: is a
    Ex:a Class Man is a Class Human ( Man is a Human )

A relationship between classes of objects

Inheritance>Composition>Aggregation>Association

USM
  • 580
  • 1
  • 10
  • 21
pohchen
  • 1,803
  • 2
  • 14
  • 12
26

Association means two classes have some kind of relationship, could be anything really.

Composition and aggregation are two kinds of associations. The easiest way to distinguish them is thinking about "how hard" the relationship is. Think about what happens when you delete the owner object.

Aggregation, the aggregated object continues to live. (Think order <-> product, the product continues to live).

Composition, the aggregated object dies with the owner. (Think paragraphs <-> document, the paragraphs die with the document).

An aggregation can be argued to be meaningless since there isn't really much difference between drawing a line with an non-filled arrow (association), and a line with a non-filled diamond (aggregation). The relations are very similar. Line with filled diamond (composition) is however very different.

Martin Algesten
  • 13,052
  • 4
  • 54
  • 77
  • 2
    correct, aggregation and compositions are distinctions related to the life cycle of the objects involved. – beluchin Dec 09 '11 at 13:34
13

This is a very arguable question. As Martin explains in the answer, the Order aggregates the Product. And this can be considered true. Grady Booch in his "Object-Oriented Analysis and Design" brings a similar example for association - a sale is associated with products in that sale, and vice versa. And a sale doesn't aggregate products. So all examples should be domain-specific, because from another point of view the association may become more specific. Another example is the composition of Documents using Paragraphs.

So everything in this field strongly depends on the context. This is the OOP.

You can try to apply your knowledge to a particular project you are going to design. I would recommend you to read Grady Booch's book, if you haven't done it yet. Lots of books have been written since it, but it is still the Bible of OO*.

khachik
  • 28,112
  • 9
  • 59
  • 94
5

UML composition, aggregation and plain association are semantic concepts, not programming concepts. The meaning of them can be understood as follows:

  • Composition: A consists of B; B is a part of A and hence cannot exist without A
  • Aggregation: A owns B, B belongs to A
  • Association: A uses B, A is related to B in a given way

(Composition and Aggregation are special types of associations.)

In Java, you may implement all of them in the same way. It's a conceptual difference.

Kos
  • 70,399
  • 25
  • 169
  • 233
  • 1
    As far as I know, when A uses B(or A knows B) - it is a dependency relationship – maks Nov 28 '10 at 19:22
  • Hmm... But don't all of the above introduce dependency? But you're right that "A knows B" is too vague to describe association. IIRC, association is between objects while dependency is between classes- correct me if i'm wrong. – Kos Nov 28 '10 at 19:54
  • According to http://www.uml-diagrams.org/class-diagrams.html#dependency "A dependency is a directed relationship used when some element or a set of elements requires (needs) other model elements for specification or implementation. It means that complete semantics of the depending elements is either semantically or structurally dependent on the the supplier element(s)". As I understand, dependency relationship can exist both between classes and between objects. – maks Nov 29 '10 at 18:35
  • The description looks to me as it's between classes - that class A's specification or implementation is somehow dependent on class B's. But it does not specify at all the way in which a single object of class A would be related to object(s) of class B - that's what associations are for. That's how I interpret it, at least. :) – Kos Nov 29 '10 at 19:54
  • @Kos: If aggregation had clear semantics, it would be a programming concept. Using the term *semantics* as an excuse not to define any is poor style (not that you are the only person in the world to do this). – reinierpost Mar 13 '12 at 10:13
2

Association is any relation between classes where instances of one class have a field reference to an instance of another class.

Composition is a "stronger" relation, meaning that one instance (parent) "owns" another one (child).

It is Aggregation that doesn't have any additional semantics other than being an association.

See more here: http://martinfowler.com/bliki/AggregationAndComposition.html

EDIT: You may add some special semantics to aggregation symbol, like "May be owned by maximum one parent at a time, but may change parents, or be an orphan." However, such extensions are your own, and are not defined in UML, as far as I know.

Goran Jovic
  • 9,418
  • 3
  • 43
  • 75
0

There seems to be some debate on which word is which.

It has to do with parent-child relationships between objects and what happens to children when you delete the parent.

One scenario says that children have no life outside that of their parent, so they should be deleted when the parent is deleted. Think "DELETE CASCADE" in foreign keys and relational databases.

The other scenario says that children should persist beyond their parents, so they should not be deleted when their parent is deleted.

I'll leave it to others to argue which word describes each situation.

duffymo
  • 305,152
  • 44
  • 369
  • 561