Java 如何在 UML 类图中表示回调

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20491501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:18:43  来源:igfitidea点击:

How to represent Callback in UML Class Diagram

javacallbackuml

提问by Nandhan

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().

现在我想知道如何设计 UML 类图。最重要的是,我需要知道如何表示将在 AnotherImplementation :: testFunction() 类中发生的回调功能。

采纳答案by nakosspy

Your code is represented in the following class diagram:

您的代码在以下类图中表示:

enter image description here

在此处输入图片说明

It represents the relationships between the classes:

它表示类之间的关系:

  • Implementationimplements ICallback
  • Implementationdepends on AnotherImplementation(it creates one in its constructor)
  • AnotherImplementationhas a ICallback(named mCallback)
  • Implementation工具 ICallback
  • Implementation取决于AnotherImplementation(它在其构造函数中创建一个)
  • AnotherImplementation有一个ICallback(名为 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:

在您的示例中,序列图testFucntion()非常简单:

sequence diagram

时序图

Note that the Implementationclass does not show in the sequence diagram. This happens because the mCallbackmember is declared as ICallback. It could be anything that implements the ICallbackinterface.

请注意,Implementation该类未显示在序列图中。发生这种情况是因为该mCallback成员被声明为ICallback. 它可以是任何实现ICallback接口的东西。

I think that the more interesting question is how to visualize the method that triggers the callback. You don't mention which method of Implementationcalls the testFunction()of AnotherImplementation, so I guess that this happens inside the constructor of Implementation. I created the following sequence diagram for this constructor:

我认为更有趣的问题是如何可视化触发回调的方法。你没有提到Implementation调用testFunction()of 的哪个方法AnotherImplementation,所以我猜这发生在Implementation. 我为此构造函数创建了以下序列图:

callback sequence

回调序列

Here you can see:

在这里你可以看到:

  1. Implementationcreates the AnotherImplementation
  2. Implementationinvokes testFunctionon AnotherImplementation
  3. AnotherImplementationinvokes informFunctionon Implementation
  1. Implementation创建 AnotherImplementation
  2. Implementation所调用testFunctionAnotherImplementation
  3. AnotherImplementation所调用informFunctionImplementation