17

In UML class diagram notation, how do I denote a top-level function that doesn't have any encapsulating class or other structure?

e.g. I have a function used for retrieving data from a URL. Since it is a function (in the math sense) of only its parameters and a shared global state, the function is not put in a class but left as a top-level method.

Now though, I need to create a UML diagram for a program using it, and I can't find anything about how to denote a top-level function. How do I do this?

2
  • You can model it as static method of a special class called e.g. global
    – xmojmr
    Nov 3, 2015 at 6:42
  • 2
    The answers below are good. However, rather than call the class or stereotype "global" I think "StandaloneFunction" is better as it is very explicit to your intent. With that said, IMO even better would be to put the "top-level" functions in a well named namespace which allows for organizing of the functions and then gives you a class to put them in. Thus, putting the function in a namespace called UrlUtilities is far superior to just having this be a "top-level" function. But then again, at this point you may as well put it in a UrlUtilities class as a static method which is the best option.
    – Dunk
    Nov 9, 2015 at 21:56

2 Answers 2

8

Create a stereotype that you can attach to the "class" classifier, e.g., <<global function>>. You can then create a single stereotyped "class" for each global function containing one fully specified static operation.

The stereotype I propose tells the person reading the diagram to interpret the classifier as a function. The rectangle (with the stereotype) on a given diagram would represent a single function, while its dependencies represent the dependencies of that single function.

2
  • I am having a difficult time seeing how your answer is different than Bart's... Nov 9, 2015 at 23:30
  • 4
    @AdamZuckerman - Bart creates a single class called "global" that aggregates all global functions, making it difficult to know which global function and dependencies apply in the give context. The stereotype I propose tells the person reading the diagram to interpret the class as a function instead. A class rectangle (with the stereotype) would represent a single function, while it's dependencies represent the dependencies of that single function. Nov 10, 2015 at 0:22
14

The UML language does not support the notion of stand-alone functions. Within UML, all functions must be part of a class.

As a work-around, you could model your stand-alone functions as static members of a class they are closely related to. If there is no such class, then you could model those functions as being part of a dummy class, like the global class mentioned by @xmojmr in the comments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.