aggregation and composition

Hi,Could somebody explain to me the difference between aggregation and composition since they are both has a relationships as far as I know.
[154 byte] By [vincmac] at [2007-9-27 20:33:53]
# 1

The instance of aggregated class can be shared by many other classes. While those classes are deleted, the instance of aggreated class won't be deleted.

In the other hand, the instance of composited class can only be used by one other class. While that class is deleted. The composited class will also be deleted.

plutian at 2007-7-7 1:36:24 > top of Java-index,Other Topics,Patterns & OO Design...
# 2
plutian's answer is good. This link can give some idea too ! http://ootips.org/uml-hasa.html- Karthicraja
karthicraja at 2007-7-7 1:36:24 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

> plutian's answer is good.

> This link can give some idea too !

> http://ootips.org/uml-hasa.html

>

> - Karthicraja

The principle which I use to differentiate the two is that

In aggregation both the instances have the same life span. An example as mentioned above being Vector and its nodes. The nodes donot exist any longer than the Vector itself.

However in composition both the instances wold rather have different life spans. e.g.,

CAR has a CARBURETOR

but the CARBURETOR is also a separate entity with a different life span. As even after one CAR no longer exists the same CARBURETOR could be used in a different CAR.

HTH.

/Khurram

nome02 at 2007-7-7 1:36:24 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

Aggregation is dynamic relationship, with composition the relation is static. These also imply (but not guarantee) that linkage in composition is bi-directional and uni-directional with aggregation, but in practice this is only a rule of thumb.

composition

A car is composed of Chassis, Engine and Wheels without all these it is not really a car.

aggregation

A car is aggregated with a spoiler and go-faster strips, without these it is still a car.

MartinS. at 2007-7-7 1:36:24 > top of Java-index,Other Topics,Patterns & OO Design...
# 5

Here is yet another example: In an AWT component, any number of event listeners may be added to receive events. The container used to contain the event listeners (probably a Vector or List) is composed by the component, but the event listeners themselves might be aggregated (since the same event listener may be added to other components, or have some other shared purpose as is the case when the component subclass implements the interface itself).

smiths at 2007-7-7 1:36:24 > top of Java-index,Other Topics,Patterns & OO Design...
# 6
>Aggregation is dynamic relationship, with composition >the relation is staticThis is a good definition
karthicraja at 2007-7-7 1:36:24 > top of Java-index,Other Topics,Patterns & OO Design...
# 7

You are correct that aggregation and composition are both "has-a" relationships. The difference is that there is a strong link between the parent and child classes in composition.

There is a weak link between the parent and child classes in aggregation.

If you destroy the parent class in an aggregation (weak) relationship, the child classes can survive on their own.

If you destroy the parent class in a composition (strong) relationship, the child classes must be destroyed also.

An example :

In aggregation - Tree is the parent class and Leaf is the child class. When you destroy the parent (Chop down the tree) some Leaves are still lying on the ground. I.E. the child (Leaf) can survive without its parent - as an entity in its own right.

In composition - Car is the parent class and Engine is the child class.

When you destroy the parent (Crush the car) the child class is destroyed too (engine is also crushed). I.E. the child class (engine) cannot survive as an entity on its own without the parent.

> The instance of aggregated class can be shared by many

> other classes. While those classes are deleted, the

> instance of aggreated class won't be deleted.

> In the other hand, the instance of composited class

> can only be used by one other class. While that class

> is deleted. The composited class will also be deleted.

CelticPhantom at 2007-7-7 1:36:24 > top of Java-index,Other Topics,Patterns & OO Design...
# 8
Hi.I think thatIn aggregation we use the object by reference.In composition we use the object by value.if you have any comments, reply pleaseHiAlex
montefusco at 2007-7-7 1:36:24 > top of Java-index,Other Topics,Patterns & OO Design...
# 9
"In aggregation we use the object by reference.In composition we use the object by value."Assuming you mean "contain" instead of "use", that's true in C++, but how do you use or contain an object by value in Java?
lhacker at 2007-7-7 1:36:24 > top of Java-index,Other Topics,Patterns & OO Design...
# 10

Hi, All,

I would like to join your discussion. In OODA and UML, there is no relationship called "composition relationship". The three well-known relationships are (1) inheritance relationship (i.e., "is" relationship), (2) association relationship ("use" relationship) and (3) aggregation relationship ("has" relationship), which is also called containment relationship. The association relationship and aggregation relationship differ in whether the relationship has a ownership or not. For instance, a car can use a gas station to fill up its tank, but it does not own the gas station. A mower can also use the gas station for fuel. Therefore, it is an association relationship. However, a car has (or owns) an engine, which is an aggregation relationship.

I think that composition means different thing. In Gamma et al's "Design Patterns" book, when they talked about code reuse, they mentioned "Inheritance versus Compsition" (p. 18). They favor object composition other than inheritance. Clearly, in their book, object composition means reusing functionality at run-time through objects acquring references to other objects, that is, through the association and aggregation relationships.

Thanks.

Tommy

MuranoYin at 2007-7-7 1:36:24 > top of Java-index,Other Topics,Patterns & OO Design...
# 11
I think reply 3 and 7 r contradicting... can some one tell me in simple terms the diff ...Sriram
planswerme at 2007-7-7 1:36:24 > top of Java-index,Other Topics,Patterns & OO Design...
# 12

> You are correct that aggregation and composition are

> both "has-a" relationships. The difference is that

> there is a strong link between the parent and

> child classes in composition.

> There is a weak link between the parent and

> child classes in aggregation.

>

> If you destroy the parent class in an aggregation

> (weak) relationship, the child classes can survive on

> their own.

>

> If you destroy the parent class in a composition

> (strong) relationship, the child classes must be

> destroyed also.

>

The difference between the two was pointed out quite simply by me in reply#3. However I dont understand how does inheritance come into the picture over here. We are discussing .........

HELLO...........

> An example :

> In aggregation - Tree is the parent class and Leaf is

> the child class. When you destroy the parent (Chop

> down the tree) some Leaves are still lying on the

> ground. I.E. the child (Leaf) can survive without its

> parent - as an entity in its own right.

>

> In composition - Car is the parent class and Engine is

> the child class.

> When you destroy the parent (Crush the car) the child

> class is destroyed too (engine is also crushed). I.E.

> the child class (engine) cannot survive as an entity

> on its own without the parent.

>

> > The instance of aggregated class can be shared by

> many

> > other classes. While those classes are deleted, the

> > instance of aggreated class won't be deleted.

> > In the other hand, the instance of composited class

> > can only be used by one other class. While that

> class

> > is deleted. The composited class will also be

> deleted.

>

nome02 at 2007-7-7 1:36:24 > top of Java-index,Other Topics,Patterns & OO Design...
# 13
Can you tell me how Java makes the different in a aggregation and a association in code?Thanks,Michiel
mgvdijk at 2007-7-7 1:36:24 > top of Java-index,Other Topics,Patterns & OO Design...
# 14
please add some Java code snippet to make it more understandable in terms that how it is implemented or identified in any given java code........thnx...
prabhakargoel at 2007-7-7 1:36:24 > top of Java-index,Other Topics,Patterns & OO Design...
# 15

>I think reply 3 and 7 r contradicting... can some one tell me in

>simple terms the diff ...

3 is completely wrong. Back to front.

7 has it correct

Composition is a stronger form of aggregation in UML which suggests that an object is composed of other objects rather than simply refering to them.

However there is nothing in the java language to describes this. It is simply a diagramatic form of explaining how object releate to eachother. In java they are always simply references.

For example

Composition

===========

Car has engine

Car has wheels

(as in example 7 - crush the car and the composed elements also get crushed)

Aggregation

===========

Car has Driver

(crush the car and hopefully the driver got out)

thebobstera at 2007-7-18 15:50:00 > top of Java-index,Other Topics,Patterns & OO Design...
# 16

I think for java users, this can be an example of composition (Carburetor) and aggregation (Person) for a car scenario.

public class Car

{

private Carburetor carb;

public Parson per;

public Person CrushCar()

{

// destroy Carburetor here, and return a clone of Person

return per.clone();

}

}

When car is crushed, person will jump out of it somehow, making a clone of itself ( see the method CrushCar() ), and client can now let the same Person sit in another Car. Making clone will assure that garbage collector will not remove this copy of Person when instance of Car goes out of scope.

Note: Person class must be cloneable.

I have not tested this code, it's just a quick example.

Let me know if I am wrong.

Khawar.

Kazim_sida at 2007-7-18 15:50:00 > top of Java-index,Other Topics,Patterns & OO Design...
# 17

> In OODA and UML,

> there is no relationship called "composition

> relationship".

Section 3.4 of the UML standard says otherwise.

A composite object represents a high-level object made of tightly-bound parts. This is an instance of a composite class, which implies the composition aggregation between the class and its parts. A composite object is similar to (but simpler and more restricted than) a collaboration; however, it is defined completely by composition in a static model.

http://www.omg.org/docs/formal/03-03-01.pdf

MartinS.a at 2007-7-18 15:50:00 > top of Java-index,Other Topics,Patterns & OO Design...
# 18

> > You are correct that aggregation and composition

> are

> > both "has-a" relationships. The difference is that

> > there is a strong link between the parent

> and

> > child classes in composition.

> > There is a weak link between the parent and

> > child classes in aggregation.

> >

> > If you destroy the parent class in an aggregation

> > (weak) relationship, the child classes can survive

> on

> > their own.

> >

> > If you destroy the parent class in a composition

> > (strong) relationship, the child classes must be

> > destroyed also.

> >

>

> The difference between the two was pointed out quite

> simply by me in reply#3. However I dont understand

> how does inheritance come into the picture over here.

> We are discussing .........

>

> HELLO...........

Hello! CelticPhantom is not talking about inheritance. he/she is talking about parent child relationships in a tree structure of objects...

The key to your answer is that you don't consider the case where you may have a tree/graph of objects. The Car/wheel example is trivial, most real applications have much more complicated data structures. If all node objects in the tree have are associated with a composition adornment then for any parent you delete/remove/unlink/make-eligible-for-gc then you should also unlink all of its children (and so on...). IMO it is not "simple".

al_bagehota at 2007-7-18 15:50:00 > top of Java-index,Other Topics,Patterns & OO Design...
# 19

Aggregation by value is composition where as Aggregation by reference is aggregation.

For composition you would use a solid diamond and for aggregation you would use a simple diamond.

e.g The relationship between CAR and wheel is aggregation sincethey have different lifespans so it would be denoted by a hollow diamond and the code would be something like

public class CAR

{

...

protected Wheel myWheel;

...

}

while the relationship of BodyFrame and CAR would be composition denoted by a solid diamond.

e.g

public class CAR

{

...

public class BodyFrame

{

...

public int weight;

public String color;

...

}

}

nome02a at 2007-7-18 15:50:00 > top of Java-index,Other Topics,Patterns & OO Design...
# 20

There is a lot of confusion here basically as people are combining definition and implementation

Definition:

Both used to model one to many or many to many relationships

Composition owns the life cycle of classes, i.e. if owner is destroyed all its composite classes will be destroyed

Aggregation is just holding a reference to the aggregated classes i.e. Even if the owner is destroyed the aggregated classes can be still available.

Note : The following code is just written to explain the problem,it can be implemented in many ways and it is not a tested code to

Design/Implementation:

This relation ship between the classes is very context sensitive, depends on the design.

Example: The relation between car and its tires can be implemented using Composition or Aggregation

Aggregation:

If in the spec the tires can exists with out a car it can be implemented using aggregation (ex A car manufacturer has a separate list of Tires, and cars)

Public Tire{ }

Public Car {

Private Vector tires = new Vector(4);

Public void addTire(Tire tt){tires.add(tires);}

}

//here Manufacturer as well as Car has a reference to the Tire

Public Manufacturer {

Public static void main (String args [])

{

Car c = new Car();

Tire t1 = new Tire();Tire t1 = new Tire();

c.addTire(t1);c.addTire(t2);

c = null;

//at this statement car is destroyed but not t1 and t2

}

}

Composition:

If in the spec the tires cannot exists with out a car (Ex A car deler does not worry maintain a list of tires , what matters to him is just the Car which has tires)

Public Car {

Car()

{

Tire t1 = new Tire();Tire t1 = new Tire();

tires.add(t1);tires.add(t2);

}

Private Vector tires = new Vector(4);

private Tire{ }

}

//here Dealer has only reference to Car

public Dealer {

public static void main(String args[])

{

Car c = new Car();

c = null;//at this statement both c as well as

}

}

seshaveeraa at 2007-7-18 15:50:00 > top of Java-index,Other Topics,Patterns & OO Design...
# 21

Hi Alex,

The statements are not entirely true.

You better view this things in following format :

Aggregation is of two types - Containment by reference and containment by value. If it is containment by reference is just aggregation and if it is by value the relationship is composition.

So :

Aggregation is - containment by reference

Composition is - containment by value

(Not object by reference or value)

ShakirMisarwalaa at 2007-7-18 15:50:00 > top of Java-index,Other Topics,Patterns & OO Design...
# 22

I like Martin Fowler's definition in "UML Distilled":

"Aggregation is the part-of relationship. It's like saying a car has an engine and wheels as its parts."

"Composition is a stronger variety of aggregation. The part object may belong to only one whole; further, the parts are usually expected to live and die with the whole. Any deletion of the whole is considered to cascade to the parts."

duffymoa at 2007-7-18 15:50:00 > top of Java-index,Other Topics,Patterns & OO Design...
# 23

Hi All,

I still have a question unanswered from the thread (UML: Composition and its implementation aspect?)

So I repeat it here:

In this thread, I see we have composition, aggregation.

Are these 2 mutually exclusive? OR can they be overlapping in respect with time line. By that I mean "Is there a case where we have a compostion at the beginning of time BUT then as we evolve with time it becomes an aggregation?"

PaulPhuoca at 2007-7-18 15:50:00 > top of Java-index,Other Topics,Patterns & OO Design...
# 24
The difference between the two is cascading delete. Unless you could figure out how to do/undo that on the fly, I'd say no. It's a design choice that's made upfront, IMO. If you decide to change, you have to rewrite your object. - MOD
duffymoa at 2007-7-18 15:50:00 > top of Java-index,Other Topics,Patterns & OO Design...
# 25

Hi duffymo, All,

::Unless you could figure out how to do/undo that on the fly, I'd say no.

::It's a design choice that's made upfront, IMO. If you decide to change, you have to rewrite your object.

So your answer is a technical-based.

So if I can figure out how to do it technically, then it's feasible. The question is then which UML element/construct will be used to model/show my question/case? We only have an aggregation (empty-diamond at the whole end) and a composition (filled-diamond at the whole end)

Now, if I or we can not figure out how to do it technically OR technically, it's simply not feasible at all, would that mean such case would not be possible in real life? and such case would not even possible at least in theory? Would that also implicitly imply the OMG/UML never think such case would exist? or such case would not even be conceivable in the first place? since there is no UML Element other than aggregation and composition.

PaulPhuoca at 2007-7-18 15:50:00 > top of Java-index,Other Topics,Patterns & OO Design...
# 26

Hi Paul,

"technical-based" - what else is there?

I'm saying that I seem to be stuck in the same rut that you've put OMG/UML into - I can't conceive of a situation where the cascade dynamic would switch in mid-stream.

But if you must have this kind of behavior, there might be a way to do it.

You could have an interface that has two different concrete implementations: one that would implement aggregation semantics and another that would use composition. You could decide which one you wanted to use at runtime.

There's also a GOF Pattern called State where an object can appear to change its type on the fly:

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

Maybe these would allow you to do what you want. I'd say that if these express what you're getting at, then it's possible to capture it in UML. - MOD

duffymoa at 2007-7-18 15:50:00 > top of Java-index,Other Topics,Patterns & OO Design...
# 27
Hi All,i want to know the Programatting defference between aggregation and composition. if any body knows please let me know how to do that.Regardsram
ram@123a at 2007-7-18 15:50:00 > top of Java-index,Other Topics,Patterns & OO Design...
# 28
can i have a code to explain this
CMDZonea at 2007-7-18 15:50:00 > top of Java-index,Other Topics,Patterns & OO Design...