databaseumlclass-diagram

How to represent an association between two classes, but two times, in a UML diagram?


I want to represent a database for football championships. How can I represent that a class "Match" is played between two classes "Team"?


Solution

  • You can represent multiple associations between the same classes in a UML class diagram by drawing separate association lines, each with its distinct role names to clarify the different relationships.

    classDiagram
        class Team {
            +List<Match> homeMatches
            +List<Match> awayMatches
        }
    
        class Match {
            +Team homeTeam
            +Team awayTeam
        }
    
        Team "1" --> "*" Match : homeMatches
        Team "1" --> "*" Match : awayMatches
    
    

    The role names "homeMatches" and "awayMatches" distinguish the two different associations between Match and Team3. This clearly shows that:

    A match involves exactly one home team and exactly one away team. A team can participate in multiple matches as either the home or away team. When implementing this in code, these associations would typically become attributes in the Match class.

    UML Diagram For 2 Classes ref: https://softwareengineering.stackexchange.com/questions/345395/does-uml-allow-multiple-relations-between-2-classes