如何使用 UML 或类定义限制专用类中的多重性和其他属性
我正在尝试创建一个 UML 类图和相应的类定义(例如,在 C# 或 Java 中),其中我有一个通用的关系层次结构和一个或多个基于该一般关系但对其进行约束的专用类。
例如,假设我有以下一般 UML 关系(假装它看起来像 UML):(
Router
<> 1
|
| *
Card
<> 1
|
| *
Port
读作:路由器包含 1 个或多个卡,每个卡包含一个或多个端口)
然后我想扩展它并定义一个专门的类,例如思科路由器。
Router <|---CiscoRouter
此外,假设 Cisco 路由器只能有 1..10 个卡。有没有办法在模型中定义这个限制?
我可以执行如下操作:
Router <|---CiscoRouter
<> 1 <> 1
| |
| * |
Card ____________|
<> 1 10
|
| *
Port
但是,从技术上讲,这会创建两个单独的 Card 对象集合,对吗?关于如何最好地定义这一点有什么建议吗?
实际设计可能会更复杂,因为我们需要 Cisco7200Router、LinkSysRouter 等类,以及自定义卡、端口等。例如:
Router <|---CiscoRouter
<> 1 <> 1
| |
| * | 10
Card <|------CiscoCard
<> 1 <> 1
| |
| * | 4
Port <|------CiscoPort
为了增加另一个复杂性,假设路由器有一个名为“权重”的属性。然而,在 CiscoRouter 中,我们实际上希望将该属性定义为某个值,例如 4 磅(例如,我们的模型以某种方式表明 CiscoRouter 重 4 磅,有 10 个卡,每个卡有 4 个端口)。我如何定义这个?
我知道我可以使用抽象类、方法等在代码中完成很多工作;但我想知道是否有任何干净的方法可以在模型或元数据中定义尽可能多的内容,以便我可以从中生成一些设计文档而不执行任何操作。
预先感谢,
丹
I am trying to create a UML class diagram and corresponding class definition (in, for example, C# or Java) where I have a generic hierarchy of relationships and one or more specialized classes that are based on that general relationship but constrain it.
For example, say I have the following general UML relationships (pretend it looks like UML):
Router
<> 1
|
| *
Card
<> 1
|
| *
Port
(Read as: Router contains 1 or more cards and each card contains one or more ports)
I then want to extend that and define a specialized class, for example CiscoRouter.
Router <|---CiscoRouter
Furthermore, let's say a CiscoRouter can only have have 1..10 cards. Is there a way to define this restriction in the model?
I could do something like the following:
Router <|---CiscoRouter
<> 1 <> 1
| |
| * |
Card ____________|
<> 1 10
|
| *
Port
However, this would technically create two separate collections of Card objects, right? Any suggestions on how to best define this?
Actual designs will probably be more complicated as we will need classes like Cisco7200Router, LinkSysRouter, etc. As well as custom Cards, Ports, etc. For example:
Router <|---CiscoRouter
<> 1 <> 1
| |
| * | 10
Card <|------CiscoCard
<> 1 <> 1
| |
| * | 4
Port <|------CiscoPort
To add another complication, let's say that the Router has a property called Weight. However, in the CiscoRouter, we want to actually define that property to be a certain value, say 4 Lbs (e.g. our model somehow states that a CiscoRouter weighs 4 Lbs, has 10 cards, and each card has 4 ports). How do I define this?
I know I can do a lot of this in code using abstract classes, methods, etc; but I want to know if there is any clean way to define as much of this as possible in the model or metadata so that I can generate some design documentation from it without executing anything.
Thanks in advance,
Dan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想说,当 UML 模型元素不容易对语义建模时,表达 UML 模型约束的最佳方法通常是使用 OCL 约束。
I would say, that generally best way to express constraints on UML models when the semantics are not easily modeled by UML model elements is to use OCL constraints.