Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Association vs. Aggregation vs. Composition in Java

Tags:

java

oop

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?

like image 796
sriram Avatar asked Feb 12 '13 17:02

sriram


People also ask

What is an association aggregation and composition?

Aggregation and Composition are subsets of association meaning they are specific cases of association. In both aggregation and composition object of one class "owns" object of another class. But there is a subtle difference: Aggregation implies a relationship where the child can exist independently of the parent.

What is the difference between composition and aggregation?

1. Association between two objects that illustrate the “has-a” relationship is called Aggregation. A composition defines a part-of a relationship, and both the entities are connected to each other. 2.

What is association vs aggregation?

Difference between Aggregation and Association:Aggregation describes a special type of an association which specifies a whole and part relationship. Association is a relationship between two classes where one class use another.

Is aggregation is same as association Write the difference between aggregation and composition?

Composition and aggregation are two types of association which is used to represent relationships between two classes. In Aggregation , parent and child entity maintain Has-A relationship but both can also exist independently.


3 Answers

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

like image 180
Anirudha Avatar answered Oct 19 '22 20:10

Anirudha


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.

like image 26
Carlos Gavidia-Calderon Avatar answered Oct 19 '22 20:10

Carlos Gavidia-Calderon


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)

like image 1
Miquel Avatar answered Oct 19 '22 19:10

Miquel