10

I have the following interface:

public interface IRegisterable
{
   T Register<T>(string username, string passw) where T : User, ICanLogin, new();
}

User is an abstract class, and ICanLogin is another interface.

Now, I want to represent the above interface with its method in a UML Class Diagram, in Visio.

How can I represent the above generic method with its constraints in a Class Diagram ?

Andreas Grech
  • 105,982
  • 98
  • 297
  • 360

3 Answers3

7

A generic class is a Template class in UML see What is the correct way to represent template classes with UML?

So can't you use thye Parameterized Class in Visio http://etutorials.org/Programming/UML/Chapter+6.+Class+Diagrams+Advanced+Concepts/Parameterized+Class/

Community
  • 1
  • 1
salgo60
  • 957
  • 5
  • 16
4

UML does not support type parametric methods directly.

About the closest you'll get is to define a nested class which has the type constraints with a stereotype which you will interpret as meaning it's a type parameter, and define the Register operation in terms of that.

+---------------------------------------------------+
|                   «interface»                     |
|                  IRegisterable                    |
+---------------------------------------------------+
| + Register (string username, string passw) : T    |
+---------------------------------------------------+

    +---------------+           +---------------+
    |  «abstract»   |           |  «interface»  |
    |     User      |           |   ICanLogin   |
    +---------------+           +---------------+
            .                           .
           /_\                         /_\
            |                           .
            +-----------+   .............
                        |   .
                +-------------------+              
                |  «typeParameter»  |
                | IRegisterable::T  |
                +-------------------+           
                | + new()           |
                +-------------------+           

    note: T is a nested class within IRegisterable
Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
2

UML DOES support parametric types (other things can be parametrized as well, and things other than types can be used as parameters as well).

Rafael Chaves
  • 553
  • 4
  • 12
  • 1
    Could you cite some "reliable source" for that? I'm just curious to learn more, especially about using "things other than types [...] as parameters as well". – Suzanne Soy Sep 11 '13 at 09:06
  • For methods and Not classes? – Andrew Scott Evans Jan 06 '17 at 22:25
  • 1
    See this blog post as "[Cliffs Notes](https://blog.abstratt.com/2007/11/01/uml-101-the-templates-package/)", the UML2 API javadoc (starting from [TemplateableElement](http://download.eclipse.org/modeling/mdt/uml2/javadoc/5.0.0/org/eclipse/uml2/uml/TemplateableElement.html)), and search the [UML specification](http://www.omg.org/spec/UML/) for the strings TemplateableElement, TemplateSignature, TemplateParameter, ClassifierTemplateParameter, OperationTemplateParameter. – Rafael Chaves Jan 08 '17 at 02:03