-1

I'm working on a class diagram for an existing application (C#) and I'm struggling with the following.

Say you have two classes A and B. Class B contains a method foo that returns a list of A objects::

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?

At first I thought this would be a one-to-many relationship where the method returns a list (one or more...) of 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.

Christophe
  • 68,716
  • 7
  • 72
  • 138
Wout
  • 77
  • 6
  • 1
    Your code is just confusing. You list `B` and right below you tell it has another operation which is not listed above. – qwerty_so Mar 05 '20 at 10:58
  • I took the liberty to edit slightly your question to address the confusion that qwerty_so has noticed in the code sample. At this occasion I also renamed the string properties.I hope you agree with the revised version – Christophe Mar 06 '20 at 08:50

1 Answers1

3

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.

This kind of relationship is called a dependency: more precisely, it is a «use» dependency. A dependency has no multiplicity: there is no difference if the B uses several times A or only once; the dependency just means that it has to know about A.

enter image description here

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 As. 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. No link, no association. The association would require something more durable, comparable to a property.

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • You added the tag `methods` but as per UML it should be `operation`. Unfortunately the term `method` is widely used instead of `operation` (even here at SO). A `method` describes behavior. See p. 287 of UML 2.5. – qwerty_so Mar 05 '20 at 21:24
  • @qwerty_so that's on purpose, because OP starts from C# code and asks how to model the method return type. Here method refers to C# and not to UML. This tag would be more useful for other people mapping some known language constructs to less known UML. – Christophe Mar 05 '20 at 21:27
  • So that's actually a C# term? - Just googled for method/operation & programming. Oh well... – qwerty_so Mar 05 '20 at 21:33
  • @qwerty_so indeed: https://learn.microsoft.com/en-us/dotnet/csharp/methods - I confess nevertheless that deep in my brain methods and operations are just another name for member functions ;-) – Christophe Mar 05 '20 at 22:18
  • 1
    I had it the same way until I got deeper into UML and found a good explanation. Things are what their name is. Ontology is quite interesting.... – qwerty_so Mar 06 '20 at 00:21