3

What does this class diagram mean? The class diagram of reflexive association uses solid lines and arrows, but here is replaced by a hollow diamond. Does it have anything to do with recursion? What will this class diagram generate? It would be best if you can give an example. In addition, this recursive relationship should be one-to-many, how to build a table if you store a database.

Christophe
  • 68,716
  • 7
  • 72
  • 138
mc_wendy
  • 43
  • 5
  • Thanks for your reply @Christophe I understand, that is to say, this kind of class diagram can describe a tree structure. In layman's terms, it can be regarded as similar to a university with multiple departments, each department contains multiple majors, and these child nodes have only one common parent node. But if you wanna design a class diagram of a Unit program, how to draw boundary classes, control classes, and entity classes to better express the essentials of object-oriented design? It would be great if I could give an example. – mc_wendy Dec 12 '21 at 02:10
  • There are many approaches. [Enity-Boundary-Control](https://en.m.wikipedia.org/wiki/Entity-control-boundary) requires use-cases to start with,because control classes correspond to use-cases and boundaries correspond to their association with actors. There are other OO design approaches as well, so it will also depend on architectural choices. But having all the classes in one diagram only works for small systems. A popular approach is therefore to start with a domain model (i.e. the entities in ECB) and use additional models to show how domain classes cooperate with UI and app infrastructure. – Christophe Dec 12 '21 at 09:24
  • But these are different questions. On StackOverflow, each question should have a very narrow focus, so that it can be answered objectively. – Christophe Dec 12 '21 at 09:28
  • I think I have a macro understanding, very grateful for that – mc_wendy Dec 13 '21 at 00:46

3 Answers3

1

Im UML, this class means that Every Unit is aggregated into some other Unit.

The UML standard doesn’t cover code generation, so interpretation of this as code depends on which tool you choose. Every proprietary code-generation tool should either stop with an error or provide an additional constructor that creates a new Unit that references itself. Otherwise, what Unit does the caller pass to the constructor for the very first Unit?

Other ways to fix the problem include taking an open-world interpretation of this UML model (where not all true information is present, as is the case for OWL), or loosening the multiplicity to 0..1.

Jim L.
  • 6,177
  • 3
  • 21
  • 47
  • Why should a code-generation tool would stop with an error: isn't it perfectly legal to have a self-referencing cycling data structure (a [circular buffer](https://en.wikipedia.org/wiki/Circular_buffer) is a perfect example, and who knows if a circular multi-buffer is not another new promising invention ;-) ). Moreover, why should a code generation tool recurse due to a reflexive association? Generating the code is not using the class instances, so potential cycle won't affect code generation. A quick test with any code generator can demonstrate this point. – Christophe Dec 11 '21 at 16:17
  • As modeled, it is an error for any Unit to not have a reference to another Unit, which is impossible (unless a Unit refers to itself). – Jim L. Dec 11 '21 at 16:27
  • The parenthetical in my previous comment made me realize I missed another possibility. I updated my answer accordingly. – Jim L. Dec 11 '21 at 16:44
  • The constructor is not in the model. So code generation would not be bothered by this aspect at all. In fact the one that I quickly tested, just generates a class with attributes. The code generator only generates what is and not what should ;-) And if a constructor needs to be added the modeler would have to specify one which is in line with the model. There is no problem to foresee one that creates self-referencing units; the operations being completely unspecified, this is anyway completely speculative. – Christophe Dec 11 '21 at 17:05
1

What does it mean?

This class diagram means that an instance of Unit can be associated with several other instances of Unit:

  • The hollow aggregation diamond is just a "modeling placebo": it does not change the meaning of the diagram, but just suggests that the association represents some kind of grouping.

  • A reflexive association means that it associates a class with itself. There is no direct relation with recursion, as this SO answer explains.

  • Nevertheless, recursive algorithms are good candidates to explore such associations (e.g. find all the instances of Unit that are indirectly related to a specific instance). In a database environment the term "recursive association" is sometimes used instead of "reflexive" because of the recursive joins that are used to implement them.

Note that the 1 should probably a 0..1 because 1 means exactly 1 and this would imply having endless cycles when navigating up.

How does it look like?

Since it's a one-to-many association, you could visualize it as a forest of trees: each Unit instance can be the start of some branches and several trees may share common branches (nonesense: there's at max one parent).

enter image description here

What is generated / How is it implemented?

Let's add some roles to better speak about the ends of the aggregation: enter image description here

Code generation will depends on the tool and target languages. But the model with the aggregation and the model with the simple association will most probably generate exactly the same code, something like:

class Unit { // Java
    private String id;
    private Unit[] child; // java objects are sharable by default 
    private Unit parent; // unless we make it non navigable in that direction
    ...
}

In an RDBMS, the table would look very similar. The relational model allows to do a bidirectional link with only one column:

ID (PK)  |  Parent (FK, nullable)
----------------------------------
w        |
u        |
u1       |  u
u2       |  u 
u3       |  u
u21      |  u2
u31      |  u3
u32      |  u3 
v        |
v2       |  v

A self-join or a recursive CTE would allow to query the data making use of the reflexive association.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Christophe
  • 68,716
  • 7
  • 72
  • 138
  • I do not concur. The hollow diamond should not be used except in a domain where you assign a special semantic. This is not the case in the OP's question. – qwerty_so Dec 11 '21 at 15:01
  • @qwerty_so I think we can agree on the fact that it's not to be recommended. However the specs define shared aggregation. And it even suggests that it can be used to represent some kind of grouping (the statement is almost copy-pasted from the specs) even if there is no specified semantic. So we cannot ignore it either. As any placebo, you may use it or not, the result is the same, but some people will feel better ;-) – Christophe Dec 11 '21 at 15:20
  • The OP asked what the meaning is, not what it should be. You show code as the model should be, not what the model is. – Jim L. Dec 11 '21 at 15:32
  • 1
    @JimL. Thanks for underlining our different approach. I see that you opted to underline the 1 multiplicity and this is well spotted. I assumed the 1 was a simple mistake and OP was more interested in the difference between with and without diamond. As I've read countless contradictory claims about aggregation vs association, I inferred that this confused most OP. Maybe my interpretation is wrong. However, the Java code shows what is AND what should be: There is no difference in a Java class between a 0..1 or a 1 multiplicity if the constructor is not shown ;-) – Christophe Dec 11 '21 at 15:55
  • 1
    @JimL. I see that qwerty_so also assumed that the aggregation was at stake and did not address the 1 inconsistency. – Christophe Dec 11 '21 at 15:58
  • There *should* be a difference in Java when the code generator is properly enforcing multiplicity. Otherwise, allowing null changes the 1 into a 0..1! – Jim L. Dec 11 '21 at 16:24
  • Also, in SQL, the OP’s model should not allow the column for the parent Unit FK to be nullable. – Jim L. Dec 11 '21 at 16:49
0

P. 110 of UML 2.5 states for this kind of relation:

shared - Indicates that the Property has shared aggregation semantics. Precise semantics of shared aggregation varies by application area and modeler.

So in short: it means what you define it should mean.


A note on history

In UML 1.5 there was only aggregation. Then in 2.0 they introduced the shared aggregation. On p. 80 of UML 2.0

Indicates that the property has a shared aggregation.

and further down

Precise semantics of shared aggregation varies by application area and modeler.

Obviously this latter sentence has not made it to the common people and you find all kinds of interpretation about what it shall mean.

Luckily now in UML 2.5 these two sentences were brought into one paragraph. Namely on my famous p. 110.

qwerty_so
  • 35,448
  • 8
  • 62
  • 86