开发者

UML - Object Method Returns a Collection

开发者 https://www.devze.com 2023-01-01 18:48 出处:网络
How can I i开发者_StackOverflow中文版ndicate 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

How can I i开发者_StackOverflow中文版ndicate 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?


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).


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.


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.


There is a indirect way of representing this.

You can define another composite class which has the composite relation of 1..* with your specific return type and use this new composite class as your return type.

For e.g. if you want to return a collection of Book class, create a new Books class with 1..* association with Book and use the Books class as your return type.

0

精彩评论

暂无评论...
验证码 换一张
取 消