如何在类图中为自定义异常关联建模? [英] How can I model a custom exception association in a class diagram?

查看:102
本文介绍了如何在类图中为自定义异常关联建模?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以获取受支持的原型(如UML 2.1)。



可以将MyClass-MyException建模为a简单的二进制关联而不是依赖关系,但它实际上在语义上不成立;它们之间没有系统的关系。与工厂创建实例的情况类似。关于依赖关系,有一篇很好的文章这里,如果您需要更多信息。


这是对类图的滥用吗?


如果您觉得有用,则不会。 UML是一种工具:在&如何帮助您,请忽略它没有帮助的地方。恰当的例子: MyClass 不会抛出任何异常, MyClass 的一个或多个方法会抛出异常。因此,依赖关系是一种抽象。但是只要:




  • 您-以及需要读取模型的其他人-都可以理解,并且

  • 这很有用



然后它不是滥用,而是使用。



hth。


Reading here, it seems modelling a custom exception class using a generalisation is common place. What it doesn't mention is how I can model an association with a class that could potentially throw the custom exception. Note, I'm not asking how to model the sequence behaviour when it comes to raising the exception; I'm specifically wanting to model the association. Or is this a misuse of the class diagram?

解决方案

Probably like this:

Note that <<throws>> isn't a standard UML stereotype. There's nothing wrong with that - it's perfectly fine to define your own. If you want to stick to standards though, <<create>> is probably the closest. (See here for list of supported stereotypes as at UML 2.1).

You could model MyClass-MyException as a simple binary association rather than a dependency but it doesn't really hold semantically; there's no systematic relationship among them. Similar to the case where a Factory creates instances. There's a good article on Dependency relationships here if you need more info.

is this a misuse of the class diagram?

Not if you find it useful. UML is a tool: use it where & how it helps, ignore it where it doesn't. Case in point: pedantically, MyClass doesn't throw any exceptions, one or more methods of MyClass throws the Exception. So the dependency is an abstraction. However as long as:

  • you - and whomever else needs to read the model - understands that, and
  • it's useful

then it's not misuse, it's use.

hth.

这篇关于如何在类图中为自定义异常关联建模?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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