Search code examples
umlcompositionmultiplicity

UML Composition multiplicity


I have a question about indicating multiplicity in an UML diagram.

I have a SpriteObject class, which has a list of animations. The SpriteObject can have 0..* animations. All animations are created inside the SpriteObject and do not exist on their own.

I'm not 100% sure how I should indicate this with multiplicity. After searching the web I have found the following 3 options:

Option 1: The multiplicity should be indicated like this, because every SpriteObject has 0 or more Animations. There is no multiplicity indicated on side of the SpriteObject since the Animation has no clue about the existence of the SpriteObject. enter image description here

Option 2: The multiplicity should be indicated on both sides like this because we need to indicate the local relationship between the two classes so 1 SpriteObject has 0 or more Animations. enter image description here

Option 3: The multiplicity should be indicated on both sides likes this, because we need to be able to read the multiplicity and understand it as part of the whole(the game). The game can contain 0..* SpriteObjects and the SpriteObject can contain 0..* Animations. That's why 0..* SpriteObjects has 0..* Animations enter image description here Can anyone tell me which option is the correct one?(if any of them is)


Solution

  • [Edit]

    It should be Option 1. This is a composition relationship

    What this means is that SpriteObject contains and manages the lifetimes of the Animation objects.

    As an example.

    public class SpriteObject : IDispose
    {
      private List<Animation> animations;
    
      // constructor
      public SpriteObject()
      {
        animations = new List<Animations>();
    
        // initialise the list
      }
    
      public ovveride Dispose()
      {
        // clear list
      }
    }
    

    If this is a an aggregation relationship.

    enter image description here

    Take note of the symbol, the hollow diamond. This is an aggregagtion relationsip. What this means is that an instance SpriteObject can have zero or more instances of Animation, but the lifetime of the Animation objects is not dependent on the lifetime of the SpriteObject.

    A C# code example would look like:

    public class SpriteObject
    {
      private List<Animation> animations;
    
      // constructor
      public SpriteObject(List<Animation> animations)
      {
        this.animations = animations;
      }
    }