P.C.
P.C.

Reputation: 649

What kind of relationship exists between these classes? Aggregation or just Association?

Please note: I know that Aggregation is a specific type of Association. But, is this aggregation or not?

class A{
     void myfunc(){
         B myB = new B();
     }
}
class B{
    // some code for class B, which as nothing to do with class A
}

So, when object of B is created and destroyed within the scope of a function of class A, what is the relationship between the 2 classes?

Upvotes: 1

Views: 573

Answers (3)

Aleks
Aleks

Reputation: 5854

This is a clear example of dependency between 2 classes, a short term relationship established in run-time. No further filosofy.

Upvotes: 1

Gangnus
Gangnus

Reputation: 24464

  • There are three aggregation states: none, shared and composition. It is not correct to use the word "aggregation" instead of "shared aggregation". Better say "shared" instead.
  • Shared aggregation is a non-strictly defined term, meaning 1:n relationship without strict containing (not composition).

I see your problem - you can call the function many times and each time the A instance will have one more B instance. So, it can be mistaken for 1:n association. But it it is not really. Look at a citation from UML standard, 2.5, p. 208: "An Association declares that there can be links between instances of the associated types". You declare an association, shown in "myfunc()". Every call to myfunc() creates a link, that belong to the same declaration, the same association. It is not one association 1:n, it is n "instances" of the same 1:1 association.

  • So, your example is n 1:1 and is none aggregation. It is not an attribute, but obviously, is navigable (one more non-strict term). So, you should show it as a usual arrow from A to B, without dots, and if you wish, you could put stereotype "local" on it.

Upvotes: 1

Gerd Wagner
Gerd Wagner

Reputation: 5673

Since you have only a reference by a local variable in a method, there is no association (and hence, no aggregation) between A and B in your example.

Otherwise, sfinnie is right with his statement (in uml-aggregation-vs-association) that "Aggregation is semantically so weak as to offer nothing practically beneficial." So, you shouldn't waste your (and our) time asking this question.

Upvotes: 0

Related Questions