Agregation vs Normal Association in Java?

Hello,I would like to know what's the main difference when I have to implement Agregation and Normal Association in Java?I would like to see an example of these OO concepts implemented in Java!!!Thanks for any help.Regards--Clayton
[281 byte] By [claytonvaldoa] at [2007-9-29 22:26:15]
# 1

I've never seen the same answer for this question twice, but here's my rough understanding, in terms of which type of association is stronger than the other:

composition > aggregation > association

Composition implies one object creates, destroys and controls the other. Aggregation implies that multiple copies of the child object are controlled by the parent, but not necessarily created or destroyed. And association, at least to me, is the super-set of both composition and aggregation. In effect, they are both associations and if you can use one of the terms in lieu of association, use that.

Lot's of other people will disagree. I don't actually think there is a "right" answer. Different UML books even give different definitions. Whatever works, just be consistent. I usually just pick one and use it throughout all the diagrams. Hasn't burned me yet.

- Saish

"My karma ran over your dogma." - Anon

Saisha at 2007-7-16 2:47:33 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

Hello,

Thanks for your answer. In fact, I think the same, but it's very confuse for myself construct different UML diagrams and in the code became all the same.

My doubt is how to represent these structures in the code, for example:

public class Tyre{

private size;

private radius;

private pressure;

...

}

public class Car{

private Tyre myTyres; // Is this an aggregation?

...

}

public class Shopper{

public buyACar(Car aCar){ // Is this an association

...

}

}

This kind of question is because, the relationship between the classes are different. I've ever had this doubt? Is an attribute considered ever an aggregation relation and when I passed an object by reference to a function is an simple association relation.

Best regards.

--

Clayton

claytonvaldoa at 2007-7-16 2:47:33 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

I would say that if class Car had a line like:

private Tyre myTires = new Tyre();

Then it would be composition, since the creation and scope of Tyre are governed by Car. I would definitely say that your second example:

public buyACar(Car)

is a dependency, not an aggregation, composition or association. Unless you assign the object to an instance/class variable, I don't think it qualifies as composition. Dependency is the loosest term of all, where you basically are saying that two classes have some level of coupling.

Again, I'm not sure of the difference between aggregation or association, but your first example:

private Tyre myTires;

is an example of aggregation/association. The Car class does not create or maintain the object (composition), but it does have a class/instance variable. If the class was only in a method (as above), it would be a dependency.

Man! Now I'm confused too. If you don't try to figure out the difference between association/aggregation, then everything else does somewhat fall into place.

- Saish

"My karma ran over your dogma." - Anon

Saisha at 2007-7-16 2:47:33 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

hi, you can think association is a super relationship of aggreagation.. something like: Composition extends Aggregations, and Agregations extends Association, Association extends Dependency.

So, if 2 class has Aggregations relationship, the two class has Assoctiation relationship, and have Dependancy, but may not have Composition.

Association is only a mark that a class has another class, eg: employer has employee, full stop. And it doesn't describe further what is the relationship between employer and employee.

Aggregations is if you have a classroom which has bunch of students. But the students may also go to sport hall, swimming pool, concert hall, etc. Thus, if there is no classroom, the student are still there, live and kicking.

Composition is like water, it has Hidrogen and Oxygen. If it doesn't have Oxygen, it is not "water"

Generally, the more specific relationship you use highlight in the diagram, the better and more readable is your design. So, telling "these class has Aggregation" is more useful than "these class has dependency"..

Alex

lexzeusa at 2007-7-16 2:47:33 > top of Java-index,Other Topics,Patterns & OO Design...
# 5

Hello Alex,

Thanks for your answer, the concepts are becaming more clearly for me now. In fact, when you see the UML diagrams, the composition is an aggregation with the arrow fullfield and a simple aggregation is an arrow empty. It what I remember from my graduation.

Do you have examples of Java for the concepts that you listted for me? I thank you so much, because it's very difficult to get some Java code in the Internet explaining these concepts.

Thanks a lot.

Best regards.

--

Clayton

claytonvaldoa at 2007-7-16 2:47:33 > top of Java-index,Other Topics,Patterns & OO Design...
# 6

>Do you have examples of Java for the concepts that you listted for me? I thank you so much, because it's very difficult to get some Java code in the Internet explaining these concepts

Aggregation example:

You have product dual LCD TV-Monitor which can function as TV and pc monitor. Now you want to put this product into 2 categories: Color TV and LCD monitor because it makes sense that the product is belong both categories.

The relationship is aggregation, because the category Color TV may contain a product, which is LCD TV-Monitor, but not necessery, because without LCD TV-Monitor, the category Color TV is exist on its own.

Composition example:

For an instance of ArticleBean (it is a simple javabean) to be displayed properly by jsp, it consists of 3 parts: header, content, and footer (which all are objects).

Here, the relation is composition, ArticleBean must have header, content, and footer in order to be displayed correctly. You can't have an article if there is no title. And generally, no article exists with a same title. So, it is safe to consider if you delete the ArticleBean, the header, content and footer are also deleted.

lexzeusa at 2007-7-16 2:47:33 > top of Java-index,Other Topics,Patterns & OO Design...
# 7

Hi,

Here is an Aggregation example:

1. UML-wise:

Using the DP as an example, the Strategy show 'aggregation' modeled by a hollow diamond.

http://patterndigest.com/

http://patterndigest.com/patterns/Strategy.html

2. Java Code-wise:

http://www.fluffycat.com/java/patterns.html

http://www.fluffycat.com/java/JavaNotes-GoFStrategy.html

PaulPhuoca at 2007-7-16 2:47:33 > top of Java-index,Other Topics,Patterns & OO Design...
# 8

Hi,

Association example:

UML-wise:

Again, using DP as an example, the Adapter pattern (Version: An object adapter relies on object composition.)

There is an association between the Adapter and Adaptee.

http://www.tml.hut.fi/~pnr/GoF-models/html/Adapter.html

Gentlemen,

Correct me if I'm wrong in Aggregation example and/or Association example.

PaulPhuoca at 2007-7-16 2:47:33 > top of Java-index,Other Topics,Patterns & OO Design...
# 9
> "My karma ran over your dogma." - AnonOff topic, but I just love that quote.
mgumbsa at 2007-7-16 2:47:33 > top of Java-index,Other Topics,Patterns & OO Design...
# 10

>Gentlemen,

>Correct me if I'm wrong in Aggregation example and/or Association example.

You are correct, adaptor and adaptee use aggregation + association...

If 2/more classes use aggregation, the classes use association because association is super relationship of aggregation.

Alex

lexzeusa at 2007-7-16 2:47:33 > top of Java-index,Other Topics,Patterns & OO Design...
# 11
it seems the duke goes nowhere ...
lexzeusa at 2007-7-16 2:47:33 > top of Java-index,Other Topics,Patterns & OO Design...