[英]How do I model (UML, Class Diagram) an object that is used in a method
I'm working on a class diagram for an existing application (C#) and I'm struggling with the following.我正在为现有应用程序 (C#) 制作类图,但我正在努力解决以下问题。
Say you have two classes A
and B
.假设您有两个类
A
和B
。 Class B
contains a method foo
that returns a list of A
objects::类
B
包含一个方法foo
,它返回A
对象的列表:
public class A { string V {get; set;} }
public class B {
string W {get; set;}
public List<A> foo(JObject bar) { /* do something */ }
}
What is the relationship that A
has with B
in this case when this is modelled in UML?在这种情况下,当用 UML 建模时,
A
与B
的关系是什么?
At first I thought this would be a one-to-many relationship where the method returns a list (one or more...) of A
.起初我以为这将是一个一对多的关系,其中该方法返回的列表(一个或多个...)
A
。 Because when you have a list attribute in a class, it is usually a one-to-many relationship.因为当你在一个类中有一个列表属性时,它通常是一对多的关系。 However, I don't know if this is the same case.
但是,我不知道这是不是同一种情况。
In the application, the A
class is only used through the method foo()
, so it would also be weird to connect it to nothing, as it would then just be a standalone class which I think is wrong too.在应用程序中,
A
类仅通过方法foo()
,因此将它连接到任何东西也很奇怪,因为它只是一个独立的类,我认为这也是错误的。
The only thing that we can say for sure is that B
uses A
: B
needs to know about A
since it returns a list thereof.我们唯一可以肯定的是
B
使用A
: B
需要知道A
因为它返回一个列表。
This kind of relationship is called a dependency : more precisely, it is a «use»
dependency .这种关系称为依赖关系:更准确地说,它是
«use»
依赖关系。 A dependency has no multiplicity: there is no difference if the B
uses several times A
or only once; A 依赖没有多重性:如果
B
多次使用A
或只使用一次,没有区别; the dependency just means that it has to know about A
.依赖只是意味着它必须知道
A
。
If you would have a one-to-many association between B
and A
, it would mean that a structural relation: at a moment in time an instance of B
could be linked with some instances of A
s.如果
B
和A
之间存在一对多关联,则意味着存在结构关系:在某个时刻, B
的实例可以与A
的某些实例相关联。 Returning one or several A is not sufficient to create a link, because each A created and returned may be disconned from the B and as soon as the A's would be returned, B could not find them back either.返回一个或多个 A 不足以创建链接,因为创建和返回的每个 A 都可能与 B 断开连接,并且一旦返回 A,B 也找不到它们。 No link, no association.
没有链接,没有关联。 The association would require something more durable, comparable to a property .
该关联将需要更持久的东西, 可与 property 相媲美。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.