Search code examples
recursionumlprivatesequence-diagramstaruml

Invoking a private method, within the object on a UML sequence diagram


So I have a class diagram here:

Class diagram

A here's my sequence diagram

Sequence diagram

I'm invoking a method in object of Newton class and in this method I have to invoke a private method fact() which returns me a factorial of a given number. This is the body of that newton() method

float Newton::newton(int n, int k) {
    return fact(n)/(fact(k) * fact(n-k));
}

As you can see fact() is invoked three times. So my question is: do I have to acknowledge (in my sequence diagram) that, fact() is used three times here, and if so - how? Or my sequence diagram is correct?


Solution

  • It depends on the purpose of the diagram:

    • Is it to document a specific implementation? In this case, you may show the calls to fact().

      Accuracy is then necessary: the arrow in a sequence diagram is not a dependency (i.e. "I'll need this function within newton()") but a message (i.e. "I'll call this function one time with those arguments"). So, as Bruno mentioned in the comments, you should then show three distinct calls (i.e. 3 times a call like in your message 5).

    • Is it to document your design? In this case the focus is more on the interactions between objects. You do not need to show private implementation detail like fact() unless the private method would be essential to understand the design. The outside world does not need to know.

    In case you're in the first case, note that fact() can be implemented recursively or iteratively. Again an implementation detail. In both case, I'd not show what's behind (is it what you tried to do with message 6?): in the case of a recursive implementation, it would make the diagram terribly complex for something easily understood in the code, whereas it would bring no additional information for the interaction scenario.