How to display a callback in a UML class diagram

I have an interface

Interface ICallback { public void informFunction(); } 

I have a class:

 Class Implementation implements ICallback { public Implementation() { new AnotherImplementation(this); } @override public void informFunction() { // do something } } 

Now consider a class in which an instance of a class implementation is passed as an interface and used to make a callback.

 Class AnotherImplementation { public ICallback mCallback; public AnotherImplementation(ICallback callback) { mCallback = callback; } public void testFunction() { mCallback.informFunction(); // Callback } } 

Now I want to know how I can develop a UML class diagram. Most importantly, I need to know how to represent the callback functionality that will be executed in the AnotherImplementation :: testFunction () class.

+7
java callback uml
source share
1 answer

Your code is presented in the following class diagram:

enter image description here

It represents the relationship between classes:

  • Implementation implements ICallback
  • Implementation depends on AnotherImplementation (it creates one in its constructor)
  • AnotherImplementation has an ICallback (named mCallback)

The class diagram does not represent the functionality of the method. The functionality of the method is visualized using a sequence or collaboration diagram.

In your example, the sequence diagram for testFucntion() very simple:

sequence diagram

Note that the Implementation class does not appear in the sequence diagram. This is because the mCallback element mCallback declared as ICallback . This may be all that implements the ICallback interface.

I think the more interesting question is how to render the method that calls the callback. You do not specify which Implementation method calls testFunction() AnotherImplementation , so I assume this happens inside the Implementation constructor. I created the following sequence diagram for this constructor:

callback sequence

Here you can see:

  • Implementation creates AnotherImplementation
  • Implementation calls testFunction on AnotherImplementation
  • AnotherImplementation calls informFunction on Implementation
+16
source share

All Articles