This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Are Setters described as  Aggregation/Composition? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Are Setters described as  Aggregation/Composition?" Watch "Are Setters described as  Aggregation/Composition?" New topic

Are Setters described as Aggregation/Composition?

Peter Kovac
Ranch Hand

Joined: Aug 08, 2010
Posts: 42

I have a code like this, where Foo is passed to Bar (e.g. by Spring wiring):

Will UML look like this or is it a simple dependency?


Matthew Brown

Joined: Apr 06, 2010
Posts: 4544

In my opinion, you can't tell from an example like that. It depends on the meanings of Foo and Bar, and more importantly on the meaning of the relationship between them. Can you consider the Foo to be part of the Bar? If so then it's probably an aggregation or a composition. So you really need a more concrete example to say anything definite.
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6529

The setter has nothing to do with composition or aggregation. The two terms are about a relationship between two or more objects. Whereas composition implies ownership, aggregation doesn't. That is, with composition, if the "owning" object goes away, so does the "owned" object. With aggregation, the aggregated object can still continue to exist even without the aggregating object. See

Junilu - [How to Ask Questions] [How to Answer Questions]
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6529

I suppose that one could argue that using a setter to establish a relationship implies one or the other but I can give examples that show this is not true. If you say that invoking a setter implies aggregation, a setter implementation could clone/copy the object passed in and "own" the clone, thus, it's composition. The reverse argument could be made in favor of aggregation. I suspect that in most cases where an object reference is passed to a setter, it does indeed establish an aggregation relationship but you should not assume that is always the case.
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

It is not as simple of translating from code to UML. UML is supposed to express your design & intention, so from conceptual point of view aggregation & composition hold different meaning..but when it is translated to code they might end up being implemented the same way. This is similar with design patterns, a lot of design patterns have similar implementation code-wise but the design pattern itself convey the meaning or intent that the designer want to solve.

Having said that, from my previous experience working on code generation portion of UML tools, typically the translation from UML to Code are:
- Aggregation/Composition implemented as an array or collection
- regular link implemented as standard single attribute of the object

Setter/getter has no direct meaning toward the relationship whether it is simple relationship or aggregation/composition, from UML design point of view it is irrelevant. Most UML tools can be customized to generate getter/setter, parameterized constructor, or both for all the important attributes.
subject: Are Setters described as Aggregation/Composition?
It's not a secret anymore!