如何在UML类图中表示回调 [英] How to represent Callback in UML Class Diagram

查看:894
本文介绍了如何在UML类图中表示回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接口说

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

在您的示例中, 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:

在这里你可以看到:


  1. 实施创建 AnotherImplementation

  2. 实施 AnotherImplementation上调用 testFunction

  3. AnotherImplementation 实施

  1. Implementation creates the AnotherImplementation
  2. Implementation invokes testFunction on AnotherImplementation
  3. AnotherImplementation invokes informFunction on Implementation

这篇关于如何在UML类图中表示回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆