Question about association classes in UML class diagram
Asked Answered
O

2

3

So I need to model the situation where we have a collection of people who are members of a federation. You can either be an active member or an inactive member of the federation. If you're inactive, you're not a member of any club. If you're an active member, you are a main member to exactly one club and can have any number of clubs that you're an internal member of.

My current UML model does not enforce that every active player should have exactly one main membership so I'm wondering how I can fix this. I personally think that I can solve this by drawing a regular association between 'Main' and 'Active' but I don't really know if this is allowed or if there are any other solutions to my problem.enter image description here

Osis answered 7/1, 2022 at 14:19 Comment(3)
Not everything should be solved using Generalization. Internal or Main seems more like a property of Membership than really a subclass. Just like 'Active' or 'Not Active' should probably not be separate subclasses of Player; but more like values of a status (of the same player)Cirrus
Your diagram demonstrates a rare knowledge of generalization sets and anti-rigid subclasses! Well done!Gastroenterostomy
@Geert: Making a status property within Membership would allow an invalid situation that will have to be debugged later, where a particular Active Player has only an Internal relationship. What is generally nice about sub properties is that they create explicit cardinality rules while providing a super property that hides differences for other purposes.Gastroenterostomy
G
1

Main is an instance of a UML Association Class because its superclass is Membership. That means Main can specialize the association (which requires another dashed line) and can subset one or both association ends. When you say

isMemberOf { subsets isMemberOf } 1

on the left end of the new association line, that means every active player must participate in exactly one Main membership, plus any number of other kinds of memberships.

You should consider renaming the subsetting and / or subsetted properties for clarity, but UML does not require it. For example,

isMainMemberOf { subsets isMemberOf } 1

(Although that name implies a person is the Main member of a club, which is not quite right.)

Gastroenterostomy answered 7/1, 2022 at 18:34 Comment(15)
I think what Geert commented would be more appropriate.Lassiter
Please see my response to his comment above, @Lassiter .Gastroenterostomy
Thanks for the nice formatting improvements, @Lassiter !Gastroenterostomy
I guess it's a question of your standpoint. I'm still in the "generalization is touchy" fraction while other's (probably you) are in the "generalization is awesome" front. Probably both have their rights. Still there are many ways to model the world.Lassiter
@qwerty_so: it’s a matter of specification (of a domain) vs realization (as code). My standpoint is the former when I use UML.Gastroenterostomy
Mee98 wrote: "every active player should have exactly one main membership". In your solution, every active player has at least one main membership. Please change the multiplicity to 1. Furthermore, your answer would be more clear if you would explicitly describe that the new assocation line shall be between Club and Active, with a dashed line to Main, not a regular assocation between Main and Active, as suggested by Mee98.Eurypterid
Thanks, I missed the max one. 🤦🏼‍♂️ I also incorporated your suggestion to improve clarity.Gastroenterostomy
I'm not familiar with the exact meaning of subsets, but I wonder if your second solution, the isMainMemberOf association, would be correct. If you add isMainMemberOf, then the original association isMemberOf still exists. Via isMemberOf, an active player would still be able to have multiple Main memberships. Am I right?Eurypterid
That would be a logical inconsistency. Think of simple sets. You can’t restrict the cardinality of a subset and still have an element of the superset that violates the restriction.Gastroenterostomy
Interesting. I always thought that "subsets" means "is subset of", but apparently it means "redefines" or "restricts the set of instances that can be associated". I can't find it in the UML specs.Eurypterid
It does mean “is subset of”, and that subset has a unique qualified name and an allowable cardinality for one type (among others). Redefinition is something slightly different: it disallows other types.Gastroenterostomy
I don't understand. I have created a new question for this: #70656350Eurypterid
Looking at the answers to my question (including your comment!) I conclude that my assumption was correct: with having isMainMemberOf, an active player can still have multiple links to the same Club, via isMemberOf, and these links may be represented by instances of Main, which violates the requirement.Eurypterid
I think even your first option, with a second isMemberOf association is invalid. It would mean that Active would have two properties called isMemberOf. In UML, it is not allowed to have two properties with the same name.Eurypterid
The two properties are distinguishable because they have different qualified names.Gastroenterostomy
C
2

Although not necesarrily wrong, you might be overusing Generalization

A Generalization is a is a relationship, and where I can follow if you say

  • a Player is a Person

but I'm having a harder time understanding

  • a Active is a Player is a Person

It seems to make more sense to model Active or NotActive as values of the Status of a Player.

Here's how the most basic model to express those requirements could look like.

enter image description here

Some pointers

  • Player becomes a role played by Person, not the same object. So if a Player is deleted, the Person object can live on. In the other direction I have modelled it using compositions. So if a Person is deleted, it's Player role is deleted, and also it's Memberships
  • Using Composition over Inheritance is a known software engineering principle
  • I've used a constraint with natural language to express the Active member should have one main membership requirement. If you like (or need) it to be more formal you could use OCL as well. An alternative option might be to add another association with [0..1] multiplicity for the main Membership. You could add a constraint there that says it's only for Memberships with property IsMainMembership = True, and that it is mandatory for Players with Status = "Active"
  • In order to insure consistency between Player.Status, and the existence of a main Membership, you could also opt to make the Player.Status derived. You could then derive the Status by checking the existence of an association to a main Membership.
  • In most implementations an instance cannot change classes. So if your Active where to become NotActive you would have to delete the Active object and create a new NotActive object. Having Active/Notactive as a status would allow you to use the same object and simply change it's status. How these statusses can be changed could be modeled using a State Machine.
  • Most OO languages also don't support multiple inheritance, so you should be careful about choosing a Generalization. If you choose to make Player a Person, you might get into trouble if you later need players that are not Persons, such as computers, or organisations, or teams. UML does support multiple inheritance, but in most organisations I've worked so far the guidelines forbid the use of multiple inheritance.
  • In this case I even did away with the association-class in favor of a regular class with regular associations. Association classes make your model harder to understand for the less UML savvy

I'm not saying this model is better or worse than your model, but it's worth think about the other end of the complexity spectrum, and where you want your model to end up at.

Cirrus answered 10/1, 2022 at 16:58 Comment(1)
Yes, I would prefer your alternative. Your answer would be more complete if you would also give your judgement about Mee98's suggestion to add an association between Active and Main. It would not be a nice solution, but would it be a valid solution? If not, is there any way to model the constraint using standard UML features instead of plain English?Eurypterid
G
1

Main is an instance of a UML Association Class because its superclass is Membership. That means Main can specialize the association (which requires another dashed line) and can subset one or both association ends. When you say

isMemberOf { subsets isMemberOf } 1

on the left end of the new association line, that means every active player must participate in exactly one Main membership, plus any number of other kinds of memberships.

You should consider renaming the subsetting and / or subsetted properties for clarity, but UML does not require it. For example,

isMainMemberOf { subsets isMemberOf } 1

(Although that name implies a person is the Main member of a club, which is not quite right.)

Gastroenterostomy answered 7/1, 2022 at 18:34 Comment(15)
I think what Geert commented would be more appropriate.Lassiter
Please see my response to his comment above, @Lassiter .Gastroenterostomy
Thanks for the nice formatting improvements, @Lassiter !Gastroenterostomy
I guess it's a question of your standpoint. I'm still in the "generalization is touchy" fraction while other's (probably you) are in the "generalization is awesome" front. Probably both have their rights. Still there are many ways to model the world.Lassiter
@qwerty_so: it’s a matter of specification (of a domain) vs realization (as code). My standpoint is the former when I use UML.Gastroenterostomy
Mee98 wrote: "every active player should have exactly one main membership". In your solution, every active player has at least one main membership. Please change the multiplicity to 1. Furthermore, your answer would be more clear if you would explicitly describe that the new assocation line shall be between Club and Active, with a dashed line to Main, not a regular assocation between Main and Active, as suggested by Mee98.Eurypterid
Thanks, I missed the max one. 🤦🏼‍♂️ I also incorporated your suggestion to improve clarity.Gastroenterostomy
I'm not familiar with the exact meaning of subsets, but I wonder if your second solution, the isMainMemberOf association, would be correct. If you add isMainMemberOf, then the original association isMemberOf still exists. Via isMemberOf, an active player would still be able to have multiple Main memberships. Am I right?Eurypterid
That would be a logical inconsistency. Think of simple sets. You can’t restrict the cardinality of a subset and still have an element of the superset that violates the restriction.Gastroenterostomy
Interesting. I always thought that "subsets" means "is subset of", but apparently it means "redefines" or "restricts the set of instances that can be associated". I can't find it in the UML specs.Eurypterid
It does mean “is subset of”, and that subset has a unique qualified name and an allowable cardinality for one type (among others). Redefinition is something slightly different: it disallows other types.Gastroenterostomy
I don't understand. I have created a new question for this: #70656350Eurypterid
Looking at the answers to my question (including your comment!) I conclude that my assumption was correct: with having isMainMemberOf, an active player can still have multiple links to the same Club, via isMemberOf, and these links may be represented by instances of Main, which violates the requirement.Eurypterid
I think even your first option, with a second isMemberOf association is invalid. It would mean that Active would have two properties called isMemberOf. In UML, it is not allowed to have two properties with the same name.Eurypterid
The two properties are distinguishable because they have different qualified names.Gastroenterostomy

© 2022 - 2024 — McMap. All rights reserved.