7

I'm trying to understand what these terms mean. And I came with few examples like:

Aggregation : Facebook has a user

Composition : Each user in facebook has a session.

Association : People uses a browser

But I'm getting confused with has a and uses a examples of mine. Why can't it be User uses a facebook account or Facebook uses a session to authenticate user?

Is that wrong in terms of OOP? Where I'm missing the concept?

sriram
  • 8,562
  • 19
  • 63
  • 82
  • [here](http://www.codeproject.com/Articles/330447/Understanding-Association-Aggregation-and-Composit) is a nice article..that would help you understand.. – Anirudha Feb 12 '13 at 17:33
  • and another [one](http://stackoverflow.com/questions/731802/what-is-the-difference-between-composition-and-association-relationship?rq=1) – Anirudha Feb 12 '13 at 17:54
  • Possible duplicate of [what-is-the-difference-between-aggregation-composition-and-dependency](http://stackoverflow.com/questions/1644273/what-is-the-difference-between-aggregation-composition-and-dependency) – nawfal Oct 03 '13 at 11:29
  • Please see a code-based example at: http://stackoverflow.com/questions/731802/what-is-the-difference-between-composition-and-association-relationship/23464244#23464244 – Almir Campos May 05 '14 at 20:07

3 Answers3

8

The uses a relationship means two things

->both can exist independently

->Data flows from the whole classifier(people) to the part classifier(browser)

The has a relationship means two things

->the lifetime of the part classifier(session) is dependent on the lifetime of the whole classifier(facebook)

->data usually flows in only one direction (that is, from the whole classifier(facebook) to the part classifier(session)).

Anirudha
  • 32,393
  • 7
  • 68
  • 89
  • 5
    Spot on. Further, Association is the generic term; Aggregation (uses a) and Composition (has a) are types of Association. – sbk Feb 12 '13 at 17:16
2

This article from the Code Project makes a neat explanation of the Subject.

First of all there's no stone-written way to model a domain, there are several ways that are correct modeling approach to a particular problem. For example, your initial example uses a Facebook class and in your follow-up you're now using a Facebook Account type: using one or another (or maybe both) has impact un your model but doesn't invalidate it.

Saying that, according to the Code Project site an association relationship implies:

  • Owner: No owner
  • Life time: Have their own lifetime
  • Child object: Child objects all are independent.

With that in mind, I don't think there's an association relationship between User and Facebook Account since they're highly dependant from each other (assuming User refers to a Facebook user), so a composition might be a better relationship.

I'm not sure what are you refering with your Session class. If it's the period of time while the user is connected there is no sense in saying Facebook uses session for authentication and maybe the asociation relationship between User and Session can be an agregation, since a User can have several Sessions in a day an it's the owner of all of them.

Carlos Gavidia-Calderon
  • 7,145
  • 9
  • 34
  • 59
1

Check this wikipedia entry that precisely discusses aggregation VS composition

So, in Java terms, a Facebook User has a name (composition) and a Facebook User has a list of friends (Aggregation)

Miquel
  • 15,405
  • 8
  • 54
  • 87
  • Your examples are fine, I changed them somewhat in hopes of making it more clear. I would only be careful with Association which, in UML terms, is correct, but put into Java, essentially means that a method from the other object is called. I'm homing in on Java because you added that tag to your question. – Miquel Feb 12 '13 at 17:09