1

For example, in java:

public class App {

    public void method() {

        Object1 o1 = new Object1(new Object2(parameters));
    }

}

I know App and Object1 have a composition relationship.

But what about App and Object2? Is it a composition as well?

Christophe
  • 68,716
  • 7
  • 72
  • 138
Kit oh
  • 29
  • 1
  • 5
  • 1
    Both are dependencies, not compositions. – Geert Bellekens Oct 01 '20 at 03:55
  • https://stackoverflow.com/questions/885937/what-is-the-difference-between-association-aggregation-and-composition – qwerty_so Oct 01 '20 at 06:58
  • Is it because Object2 is used in `method` that it's a dependency relationship? So if o1 was created as a variable outside `method` then it would be a composition? @GeertBellekens – Kit oh Oct 01 '20 at 07:48
  • It's only a composition if there is a *structural* relation. The relation here is temporary, and exists only during the execution of `method` – Geert Bellekens Oct 01 '20 at 07:51
  • Does this answer your question? [What is the difference between association, aggregation and composition?](https://stackoverflow.com/questions/885937/what-is-the-difference-between-association-aggregation-and-composition) – Geert Bellekens Oct 01 '20 at 07:51
  • Definitely helped, along with this article: http://usna86-techbits.blogspot.com/2012/11/uml-class-diagram-relationships.html – Kit oh Oct 01 '20 at 10:27
  • One thing though, what, to you, distinguishes associations from aggregations when implementing code? Also, thanks a bunch @GeertBellekens – Kit oh Oct 01 '20 at 10:27
  • Aggregations have (almost) no technical impact. They are mostly a functional concept. The only real constraints for aggregation are: have to be binary (only two ends) and they have to be acyclic (so they cannot become a part of themselves) – Geert Bellekens Oct 01 '20 at 10:57

1 Answers1

2

Using a class in a method is not sufficient for an association

Your class App has no fields of class Object1 or Object2. It just uses Object1 and Object2 in the implementation of a method. This is not sufficient for making an association: there is no conceptual relationship between App and ObjectX; it's just an implementation detail. And since composition is a special kind of association, there is no composition either.

Using a class means a dependency

Since your App uses Object1 and Object2, there is a «use» dependency: App needs to know these classes. You could show this dependency with an open headed dashed arrow.

However, the dependency in your example is only at the level of the method implementation and not at the level of the class itself. You could implement the method otherwise. I'd therefore advise not to show such a volatile dependency in your model. The dependency would be advisable if the class definition itself would use such an object (e.g. if a method would return an ObjectX or use an ObjectX parameter).

Terminology: Not all compositions are compositions!

As explained, there is no composition here. Nevertheless, the word is ambiguous:

  • it could mean object compostion. This is just about objects having fields of another class.
  • it could mean UML composition. This is a special kind of association with exclusive ownership
Christophe
  • 68,716
  • 7
  • 72
  • 138
  • Thanks @Christophe. "I'd therefore advise not to show such a volatile dependency in your model" Regarding it being 'volatile'; would a nonvolatile implementation be to turn it into an actual composition relationship between App and Object1? – Kit oh Oct 02 '20 at 05:12
  • @AMoses no, I used the term "volatile" to mean that the implementation could change independently of the class’ structural characteristics. Your method implementation describes a behavior of the class, and a class diagram describes the structure of the classes independently of their behaviors. – Christophe Oct 02 '20 at 05:37
  • UML 2.5 does not have «use» but only `Usage`. This is heritage, isn't it? – qwerty_so Oct 02 '20 at 08:53
  • @qwerty_so You really made me hesitate. I think the "usage dependency" is still noted with a `«use»` label on the dashed dependency line. I preferred to provide the ready to use keyword between guillemet rather than the metamodel element without guillemet in my answer. I don't think it's heritage, unless page 746 of the UML 2.5.1 specs was replaced without me noticing. – Christophe Oct 02 '20 at 17:59
  • @qwerty_so now I'm reinsured: page 39, there is a caption: "Figure 7.20 An example of a «use» Dependency" ;-) – Christophe Oct 02 '20 at 18:02
  • The missing glossary in the UML specs is a real PITA. A bit above the picture it states _A Usage is shown as a Dependency with a «use» keyword attached to it._ That keyword itself is not explained like e.g. `trace`. Well... – qwerty_so Oct 02 '20 at 20:08