问题描述
我有一个接口说
Interface ICallback {
public void informFunction();
}
我有一个班级说:
Class Implementation implements ICallback {
public Implementation() {
new AnotherImplementation(this);
}
@override
public void informFunction() {
// do something
}
}
现在考虑一个类,其中Class Implementation的实例作为接口传递并用于进行回调。
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
}
}
现在我想知道我怎么做设计一个UML类图。 最重要的是,我需要知道如何表示将在类AnotherImplementation :: testFunction()中发生的回调函数。
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().
推荐答案
您的代码在以下类图中表示:
Your code is represented in the following class diagram:
它表示类之间的关系:
-
实施
实施ICallback
-
实现
取决于AnotherImplementation
(它在构造函数中创建一个) -
AnotherImplementation
有一个ICallback
(名为mCallback)
Implementation
implementsICallback
Implementation
depends onAnotherImplementation
(it creates one in its constructor)AnotherImplementation
has aICallback
(named mCallback)
类图不代表方法功能。使用序列或协作图可视化方法功能。
A class diagram does not represent method functionality. Method functionality is visualized with a sequence or a Collaboration diagram.
在您的示例中, testFucntion()的序列图
非常简单:
In your example, the sequence diagram for testFucntion()
is very simple:
请注意,序列图中未显示 Implementation
类。发生这种情况是因为 mCallback
成员被声明为 ICallback
。它可以是任何实现 ICallback
接口的东西。
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.
我认为更有趣的问题是如何可视化触发回调的方法。你没有提到 Implementation
的哪种方法调用的另一种实现
code>,所以我猜这发生在的测试
Implementation
的构造函数中。我为这个构造函数创建了以下序列图:
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:
在这里你可以看到:
-
实施
创建AnotherImplementation
-
实施
在AnotherImplementation上调用
testFunction
-
AnotherImplementation
在实施
Implementation
creates theAnotherImplementation
Implementation
invokestestFunction
onAnotherImplementation
AnotherImplementation
invokesinformFunction
onImplementation
这篇关于如何在UML类图中表示回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!