File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes composite pattern and composition vs aggregation confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "composite pattern and composition vs aggregation confusion" Watch "composite pattern and composition vs aggregation confusion" New topic
Author

composite pattern and composition vs aggregation confusion

Tudor Raneti
Ranch Hand

Joined: Nov 29, 2009
Posts: 145
I have gotten composition vs aggregation facts down from HERE

composition vs aggregation relationship
inheritance: is-a
composition: part-of
aggregation: has-a

difference is in lifetime management:
composite: contained object lifetime is "managed"* by the container object
class A {
B b = new B();
}
aggregation: contained object lifetime is NOT "managed"* by the container object
class A {
B b;

A (B b) {this.b = b}
}
B b = new B;
A a = new A(b);

*it's about when the composite or aggregate gets garbage collected

But then I read about Composite Design Pattern
HERE
where they mention "1-to-many “has a” " and call the object composite everywhere. Does this mean an object is a composite although it contains clearly an aggregation? It does show the UML for aggregation too on their page, and it does behave like an aggregate, then why is it still called a composite. Wiki is the same on this HERE, with their schema saying about aggregation but mentioning composite.
Because in their example they use an ArrayList where they aggregate the components, the aggregated components don't get garbage collected if the "composite" is destroyed. That enforces the facts i mentioned at first even more, while in contradiction with them.

So again which is what? Derr... hmph Oo
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

It's probably easier if you think of it as

Aggregation: has-a
Composition: owns-a

Composition is a special form of aggregation. When A has B, A doesn't necessarily own B. When A owns B, that implies A and only A has B.
Tudor Raneti
Ranch Hand

Joined: Nov 29, 2009
Posts: 145
Exactly. When A owns B, that implies A and only A has B. In their example A has an array (or aggregation) of B references, whose lifetimes don't end with the destruction of A.

I see one way out of this is:
the composite pattern is wrongly named; it should be named recurrent composition and have a correspondent recurrent aggregation, in relation to composition vs aggregation perhaps. So in their example, using aggregation would make that recurrent aggregation design pattern. It's a lot more meaningful.

EDIT
After I read up Decorator pattern I realized that it should be called composite decorator pattern instead (or aggregate decorator pattern), or composite enhancer pattern to be more meaningful.

More semantic notes: aggregation means a group while composition means the act of combining parts or elements to form a whole, which means there is nothing wrong with the parts being aggregates. In fact the whole difference is in whether the container manages the whole composition life time or not (according to most sources) - so it's an OOP technical difference, in reality there seems to be no difference other than the context where they should and are preferably used. An aggregation is a composition of entities, making the whole, the aggregate that is, while the composition is made out of entities, which themselves can be aggregates (or composites). They are synonyms.
TIDE

Other sources:
webpage

Composition takes the aggregation relationship one step further. I like to think of composition as aggregation with responsibilities. It ensuring that the containing object is responsible for the lifetime of the object it holds. If Object B is contained within Object A, then Object A is responsible for the creation and destruction of Object B. Unlike aggregation, Object B cannot exist without Object A.


webpage

In an object-oriented design of a Java program, the way in which you model objects that contain other objects is with composition, the act of composing a class out of references to other objects. With composition, references to the constituent objects become fields of the containing object.

For example, it might be useful if the coffee cup object of your program could contain coffee. Coffee itself could be a distinct class, which your program could instantiate.


webpage

Composition is generally used when you want the features of an existing class inside your new class, but not its interface. That is, you embed an object so that you can use it to implement functionality in your new class, but the user of your new class sees the interface you’ve defined for the new class rather than the interface from the embedded object. For this effect, you embed private objects of existing classes inside your new class.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Egh, it's just a pars pro toto. I wouldn't worry about it too much. You seem to understand what's going on, and they're just using it as a figure of speech. Probably because the word composition is closer to daily speech than aggregation.
Tudor Raneti
Ranch Hand

Joined: Nov 29, 2009
Posts: 145
That's exactly that. Those figures of speech that get you past an annoying interviewer or not.

I came across another contradiction HERE:
The authors further distinguish between 'Aggregation', where one object 'has' or 'is part of' another object (implying that an aggregate object and its owner have identical lifetimes) and acquaintance, where one object merely 'knows of' another object. Sometimes acquaintance is called 'association' or the 'using' relationship.

From the authors of Design Patterns: Elements of Reusable Object-Oriented Software

There's no mention of composition. Instead they introduce 'aquaintance' for what I think it's really delegation. It would seem that pattern guys have their own version of the story. This is beginning to look like politics.

One final note about the composition pattern. I'm pretty sure now that they name it composite pattern because of the objects that inherit 'common denominator' behavior for once, and are made of a whole-part hierarchy and all the other traits of the pattern. They weren't as nit picky when they named it.

} Finally, {
THIS page seems to hold all the documentation needed.
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: composite pattern and composition vs aggregation confusion