UML中基数、关联与泛化的协作及Player-Field场景建模咨询
UML Class Modeling: Cardinalities, Relationships, and Generalization for Player & Field
First: Will Incorrect Modeling Lead to 9 of Each Subclass?
Absolutely—if you model separate associations between Player and Endfield, plus Player and Startfield, each with a multiplicity of 9, that’s exactly what’ll happen. UML treats these as independent constraints: one requiring 9 Endfields, another requiring 9 Startfields, totaling 18 instances instead of the 9 you need. That’s why we need to anchor the multiplicity to the parent Field class, not its subclasses.
Correct Modeling Approach
Let’s break this down step by step:
Build the Generalization Hierarchy
- Mark
Fieldas an abstract class (italicize its name in UML or add the{abstract}stereotype to make this explicit). - Draw generalization arrows (empty triangle pointing toward the parent) from
EndfieldtoField, and another fromStartfieldtoField. This tells UML that both subclasses are valid, concrete implementations of the abstractField.
- Mark
Create the Player-Field Association
- Draw an association line between
PlayerandField. - On the
Playerend, set multiplicity to1(each group of fields belongs to exactly one player). - On the
Fieldend, set multiplicity to9(each player owns exactly 9Fieldinstances total). - Since
Fieldis abstract, these 9 instances must beEndfieldorStartfieldobjects—UML prevents instantiating abstract classes, so the subclasses automatically fill this role.
- Draw an association line between
Add Clear Constraints (Optional but Recommended)
- To eliminate any ambiguity, attach a UML constraint (enclosed in curly braces) to the association or
Playerclass. For example:
(AssumingPlayer { self.fields->size() = 9 }fieldsis the name of the attribute inPlayerthat holds theFieldinstances.) - If you want to add optional rules (like "at least 1 Startfield per Player"), extend the constraint:
Player { self.fields->size() = 9, self.fields->select(oclIsTypeOf(Startfield))->size() >= 1 }
- To eliminate any ambiguity, attach a UML constraint (enclosed in curly braces) to the association or
Key Mistakes to Skip
- Don’t link Player directly to subclasses: This creates separate multiplicity rules for each subclass, breaking your "total 9" requirement.
- Don’t skip marking Field as abstract: If you leave it concrete, someone might accidentally instantiate a raw
Fieldobject, which defeats your subclass-only intent.
内容的提问来源于stack exchange,提问作者Heuwinkelsen
相关产品推荐
相关产品推荐

