UML Design类图:具有另一个类作为属性的类? [英] UML Design class diagram: Class with another class as attribute?

查看:802
本文介绍了UML Design类图:具有另一个类作为属性的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难解决如何将特定场景建模为UML设计类图的情况.

I'm having a pretty hard time trying to figure out how to model a certain scenario as a UML design class diagram.

假设我有以下情况:

我有一个名为CPoint的类,它具有两个属性:x和y(R2平面中的坐标).另外,我有一个名为CLine的类,该类应具有两个CPoint作为属性.

I have a class named CPoint that has two attributes: x and y (coordinates in a R2 plane). Additionally, I have a class named CLine that should have two CPoint as attributes.

这对代码来说非常简单(我将在示例中使用C ++):

This is pretty straight forward to code (I'll use C++ in my example):

class CPoint{
    float x;
    float y;
    //Constructor, gets and sets here
}

对于CLine:

class CLine{
    CPoint p1;
    CPoint p2;
    //Constructor, gets and sets here
}

现在我的问题是:如何在UML中建模这样的东西?

Now my question is: How do I model such a thing in UML?

我想到了类似的东西:

但是后来我被告知这违反了面向对象建模的原理,所以我做到了:

But then I was told that this is violating the principles of object oriented modeling, so then I did this:

但是它一点也不能说服我.另外,我在阅读有关设计模式的同时,在阅读有关单例的同时也参加了UML设计:

But it does not convince me at all. Additionally, I was reading about design patterns and came to this UML design while reading about singletons:

这使我认为我的最初方法是正确的.另外,如果我将其视为C ++程序,我可以看到我的第一种方法就可以了.但是,在Java中,我仍然必须通过在CLine的构造函数中执行new CPoint(0, 0)来创建对象.我对此很困惑.

Which makes me think my initial approach was just right. Additionally, I'm able to see that my first approach is just alright if I think about it as a C++ program. In Java, however, I'd still have to create the object by doing new CPoint(0, 0) in the CLine's constructor. I'm really confused about this.

那么,我该如何模拟这种情况?当我尝试对情况建模时,我也许太具体了吗?

So, how do I model this situation? Am I perhaps being too concrete when I attempt to model the situation?

提前谢谢!这不是让我晚上睡觉

Thanks in advance! This isn't letting me sleep at night

推荐答案

在UML中,关联或属性(属性)或多或少是同一件事,因此它们都是正确的.

In UML an association or an attribute (property) are more or less the same thing, so they are both correct.

在大多数UML 工具中,它们是不同的东西.

In most UML tools however they are different things.

这里并没有真正的规则,但是有最佳实践. 我的 UML最佳实践:属性或关联说:

There is not really a rule here, but there are best practices. My UML Best Practice: Attribute or Association says:

使用关联的类和属性的数据类型

Use Associations for Classes and Attributes for DataTypes

这篇关于UML Design类图:具有另一个类作为属性的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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