views:

133

answers:

3

How can I indicate that a method will return a collection of objects in UML? Is there a better way to explain the relationship than to have a collection class as a return type?

+1  A: 

You may consider to use an "association". There are three basic types of association: composition, aggregation and "normal" association. Each of them expresses a different existential dependency of the whole to the part and vice versa.

Usually associations are expressed by connecting two classes by a line. Composition and aggregation have a diamond symbol at the composite's/aggregate's side. The composite/aggregate consists of one or more parts. (See the wiki-article)

Example:

Immagine you have two classes: Library, Book. We can say the Library is the whole and Book the part. We could notate it like this (in ASCII, please google for real diagrams).

Library (Aggregate) <>--- Book (Part)

If you want to express these relationships association will be your friend.

EDIT:

As I said in the comment, I don't think there is a special notation for returned collections. However, you're right, returning a Collection is kinda language specific. But you may consider to return an array (String[]), which is a more general way to represent a set of values and should be more language independent than a Collection. It's then up to the programmer how he implements it. He may use a Collection, a C++ STL vector ... the point is: return a set of values.

Simon
Right, but how would I indicate that a method is returning that association?
derekerdmann
Usually a method defines the __behaviour__ of a class. So there is no way to express that the method returns a collection by using a class diagram (except from indicating its return value).
Simon
So then I would just say that the method returns Collection<String> (in Java)? I thought using that kind of language-specific notation was generally avoided in UML.
derekerdmann
@derekerdmann See edit
Simon
Makes sense. Thanks for your help!
derekerdmann
A: 

Hmm, it is true, that in UML class diagrams there is no explicit way to express collection types. I read a book where they criticized UML for that, but they mentioned, that in some eralier versions of UML there were collections types. Another thing is the Object Constraint Language (OCL), which is integrated with UML (as OMG standard) and has its own collection types with nice operations.

Gabriel Ščerbák
+3  A: 

I am coming to this a little late, since I was looking for an answer to a similar question. I am writing this incase somebody else is looking for a similar answer.

Do you want to indicate that a method returns a collection of a specific type? If so, for a method you should be able to set the type of the return parameter and the multiplicity of the return parameter to 0..* or 1..*. This would indicate that the method returns the specified type and that it has whatever multiplicity you state.

E.g. using the Library <>--- Book example, suppose there is a method on Library called GetBooks that takes a string parameter, author name, and returns a collection of Book instances. The UML would look something like this:

Library +GetBooks(authorName: string) : Book[0..*]

Your UML diagramming tool should support this; I am using Magic Draw. This UML states that GetBooks returns 0 or many Book instances. It is now up to programmer to decide how to implement the return parameter in the implementation language (as stated above by Simon).

RobertMS