2

I have to write collaboration diagram for this piece of code:

public static void main(String[] args){
Playlist list = new Playlist();
list.add(new mp3("song1",5));
list.add(new wav("song2",6));
list.add(new mp3("song3",7));

list.play();

Where mp3 and wav are classes inherited from class Track and they have constructor with two arguments. Also, function "add" takes one argument which is type Track. So, what I think is that we should have 3 self calls(for every call of method add) because that is function from class list, and then one(also self call) for function play. But not sure how to include objects of class mp3 and class wav because we are only calling functions from class Playlist. So, I am not sure why are we calling function play from class mp3(wav) and not from class playlist. And from the other side we are calling function play from playlist.

Christophe
  • 68,716
  • 7
  • 72
  • 138
Marc
  • 45
  • 5
  • Please show what you have already and where you are stuck. – Geert Bellekens Nov 29 '21 at 15:43
  • Just have edited the post, please see now – Marc Nov 29 '21 at 15:52
  • 2
    The diagram does not correspond to the code at all. – Geert Bellekens Nov 29 '21 at 15:56
  • That is what I think as well(not my solution). So I think that we should have class Playlist and 4 selfcalls(3 for add and one for play). But I am not sure what to do with arguments of function add. Should I represent them as well on some way? – Marc Nov 29 '21 at 16:00
  • 1
    Hi Marc and welcome on SO. Would you mind calling this "Communication diagram", for consistency with the UML versions of the last 15 years ? ;-) – Christophe Nov 29 '21 at 20:08
  • Nope, Communication diagram is OK :). Is my reasoning about this problem correct, and any help about part I am not sure of(mentioned above)? – Marc Nov 29 '21 at 20:26

1 Answers1

2

The example code only shows what the main function does. Therefore, all links should start in main.I would draw the following links:

  • "1 Playlist()", from main to list:Playlist
  • "2 mp3("song1",5)", from main to anonymous1:mp3
  • "3 add(anonymous1)", from main to list:Playlist
  • "4 wav("song2",6)", from main to anonymous2:wav
  • "5 add(anonymous2)", from main to list:Playlist
  • "6 mp3("song3",7)", from main to anonymous3:mp3
  • "7 add(anonymous3)", from main to list:Playlist
  • "8 play()", from main to list:Playlist

However, the UML specification is not clear about

  1. How to represent a global main function in a communication diagram.
  2. How to represent calling a constructor in a communication diagram.
www.admiraalit.nl
  • 5,768
  • 1
  • 17
  • 32
  • But, shouln't messages come from objects, and main is not object. Are we looking class where main is? Is that what is main? – Marc Dec 03 '21 at 15:31
  • You are right, main is not an object. UML does not have a representation for functions in the global scope, like main, as far as I know. But main is definitely the origin of all messages, so the best thing we can do is draw main as if it were an object. – www.admiraalit.nl Dec 03 '21 at 18:46
  • OK, and if we had for example, class List and then function add in that class, then that add function would come from object of class List right? – Marc Dec 03 '21 at 20:19
  • The arrow points from the caller to the callee. That's why I wrote that `add` goes from `main` to `list:Playlist`. Suppose class Playlist contains `private songs: List;` `public add(s: Song) { songs.push(s); }`, then we have a link from `list:Paylist` to `songs:List`, with arrow `push(s)`. – www.admiraalit.nl Dec 05 '21 at 15:25
  • Small questions: shouldn’t the new message rather be <> messages (or at least bear the name of the constructor) ? – Christophe Dec 05 '21 at 22:38
  • 1
    @Christophe, thank you, yes, that is better. I have edited my answer. However, the UML specification is not clear about how to represent a constructor call in a communication diagram. – www.admiraalit.nl Dec 06 '21 at 11:31
  • 1
    @Christophe, sorry, but I have removed the «create» stereotypes again after re-reading the meaning of this stereotype in the UML specs. – www.admiraalit.nl Dec 06 '21 at 19:57
  • @www.admiraalit.nl fine for me (we have the name of the constructor instead). – Christophe Dec 06 '21 at 21:34
  • 1
    @www.admiraalit.nl btw, we do not know the context, but the `main()` function could be a static operation of a containing class (e.g. java, C#) or a [class corresponding to a specialized functor](https://stackoverflow.com/a/63915062/3723423) (e.g. C++) - On the messages here a quote of Booch&al's "UML user guide": "Within a sequence diagram, the lifetime, creation, and destruction of objects or roles are explicitly shown by the vertical extent of their lifelines. Within a communication diagram, creation and destruction must be indicated using notes" – Christophe Dec 06 '21 at 21:53