I have an Interface say
Interface ICallback {
public void informFunction();
}
I have a Class say:
Class Implementation implements ICallback {
public Implementation() {
new AnotherImplementation(this);
}
@override
public void informFunction() {
// do something
}
}
Now consider a class where in the instance of Class Implementation is passed as a interface and is 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 design a UML Class Diagram. Most importantly I need to know how to represent Callback Functionality that will happen in the Class AnotherImplementation :: testFunction().
An asynchronous message is used when the message caller does not wait for the receiver to process the message and return before sending other messages to other objects within the system. The arrowhead used to show this type of message is a line arrow as shown in the example below.
ref Reference: refers to an interaction defined on another diagram. The frame is drawn to cover the lifelines involved in the interaction. You can define parameters and a return value.
alt is used to describe alternative scenarios of a workflow. Only one of the options will be executed. opt is used to describe optional step in workflow. For example, for online shop purchase sequence diagram you may use opt to describe how user can add gift wrapping if she wishes.
Your code is represented in the following class diagram:
It represents the relationships between the classes:
Implementation
implements ICallback
Implementation
depends on AnotherImplementation
(it creates one in its constructor)AnotherImplementation
has a ICallback
(named mCallback)A class diagram does not represent method functionality. Method functionality is visualized with a sequence or a Collaboration diagram.
In your example, the sequence diagram for testFucntion()
is very simple:
Note that the Implementation
class does not show in the sequence diagram. This happens because the mCallback
member is declared as ICallback
. It could be anything that implements the ICallback
interface.
I think that the more interesting question is how to visualize the method that triggers the callback. You don't mention which method of Implementation
calls the testFunction()
of AnotherImplementation
, so I guess that this happens inside the constructor of Implementation
. I created the following sequence diagram for this constructor:
Here you can see:
Implementation
creates the AnotherImplementation
Implementation
invokes testFunction
on AnotherImplementation
AnotherImplementation
invokes informFunction
on Implementation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With