Skip to content
IRC-CodingIRC-Coding
UMLClass DiagramSequence DiagramActivity DiagramOCLCardinality

UML Fundamentals: Diagrams, Relationships & Cardinality

Learn UML essentials: structural and behavioral diagrams, aggregation, composition, cardinality, OCL, and exam preparation strategies.

S

schutzgeist

9 min read
UML Fundamentals: Diagrams, Relationships & Cardinality

UML (Unified Modeling Language)

This post is a definition guide for UML — including exam questions, core components, and key concepts.

In a Nutshell

UML is a standardized visual language for modeling software and systems. It helps you document requirements, design, and communication in a consistent way.

Core Technical Overview

UML separates structure from behavior:

  • Structural diagrams (for example: class diagram, package diagram, component diagram)
  • Behavioral diagrams (for example: activity diagram, sequence diagram, state diagram)

Relationships:

  • Association
  • Aggregation
  • Composition
  • Generalization

Cardinalities (multiplicities) describe relationships (e.g., 1, 0..1, 0..*). With OCL, you can formally specify constraints (invariants, preconditions, postconditions) — these can be directly converted into test cases.

Exam-Relevant Points

  • Structural vs. behavioral diagrams: UML divides diagrams into these two main categories. Structural diagrams show the static layout (e.g., class diagram), behavioral diagrams show dynamic execution flow (e.g., sequence diagram).
  • Distinguish relationships correctly: Association, aggregation, composition, and generalization use different notation and have different semantics. Aggregation and composition are particularly easy to confuse on exams.
  • Cardinalities are a common exam focus: Multiplicities like 1, 0..1, 1..*, or * describe how many objects participate in a relationship. Incorrect cardinalities distort the model.
  • Use OCL for precise constraints: The Object Constraint Language expresses formal constraints, preconditions, and postconditions. You can derive test cases directly from OCL expressions.
  • Model states with guards: Guards in state diagrams are conditions that must be satisfied for a state transition to occur. They clarify a system’s behavior.
  • Version, review, and approve models: UML models are living documents. They must be versioned, regularly reviewed, and kept in sync with requirements to maintain their value.

Core Components

  1. Class/Attribute/Operation + Visibility — A class models a type of objects. Attributes describe data; operations describe behavior. Visibility markers like +, -, #, and ~ control access (public, private, protected, package).
  2. Relationships (Association, Aggregation, Composition) — Association is a loose connection. Aggregation is a “has-a” relationship with weak lifecycle dependency. Composition is a strong lifecycle dependency where parts exist only as part of the whole.
  3. Multiplicities + Roles — Multiplicities specify how many objects of one class can link to objects of another. Roles name the function of a class within a relationship, such as “Customer” and “Order”.
  4. Packages/Namespaces — Packages group related model elements and reduce complexity in large models. They clarify namespaces and responsibilities.
  5. Components/Interfaces — Component diagrams show software building blocks and their interfaces. They’re particularly useful for architecture modeling and showing dependencies.
  6. Activity (Decision, Parallelism) — Activity diagrams model business processes and workflows. Branches, decisions, and parallelization with Fork/Join are shown here.
  7. Sequence (Lifeline, Message) — Sequence diagrams show interactions between objects over time. Lifelines represent objects; messages represent communication, either synchronous or asynchronous.
  8. State Machine (Guard, Entry/Exit) — State diagrams describe object behavior through states and transitions. Entry, exit actions, and guards specify when states are entered, exited, or switched.
  9. Stereotypes/Profiles — Stereotypes extend UML elements with domain-specific meanings. Profiles collect stereotypes and rules for specific application areas, such as real-time systems.
  10. OCL + Test Derivation — OCL allows precise, formal statements about models. Invariants, preconditions, and postconditions can be directly used as the basis for automated tests.

Practical Example (Library)

Class Diagram:
Reader 1..* Loan
Medium 1..* Loan
Loan: startDate, endDate

OCL Invariant:
endDate > startDate

Advantages and Disadvantages

Advantages

  • Shared language between business and technical teams
  • Earlier error detection
  • Better test derivation
  • Clear documentation

Disadvantages

  • Learning curve
  • Risk of overly complex models
  • Loses value without maintenance

Typical Exam Questions (with Brief Answers)

  1. Main categories of UML diagrams? Structural and behavioral diagrams.
  2. Aggregation vs. composition? Aggregation is weak; composition binds lifecycle.
  3. What is OCL used for? Formal constraints → tests.

Free Response

For exams: fewer diagrams done correctly and consistently beats many sloppy ones. Cardinalities and relationships must match business rules. Models should be versioned and linked to requirements and test cases.

Learning Strategy

  1. Understanding Foundation: Compare a concrete class diagram with a sequence diagram for the same use case.
  2. Deepening Method: Model a small project using multiple diagram types and check for consistency.
  3. Exam-Focused Training: Practice distinguishing aggregation from composition and working with typical cardinalities.
  4. Error Prevention: Keep naming, multiplicities, and relationships consistent across all diagrams.

Practice Example 1: Class Diagram for a Library

Model the classes Reader, Medium, and Loan. A Loan connects a reader and a medium. The multiplicities are 1..* between Reader and Loan, and between Medium and Loan, because a reader can have multiple loans and a medium can appear in multiple loans.

Practice Example 2: Recognizing Aggregation vs. Composition

A university has faculties (aggregation: faculties can exist independently). A car has an engine (composition: without the car, the engine in this context ceases to exist). Aggregation is weak; composition binds the lifecycle.

Practice Example 3: Formulating an OCL Invariant

A bank transfer has the invariant: amount > 0. In OCL: context Transfer inv: self.amount > 0. From this, you can derive a test case that marks negative amounts as invalid.

Practice Question 1: Determine the Diagram Type

Which UML diagram shows the exchange of messages between objects over time?

Answer: The sequence diagram. It displays lifelines and messages in temporal sequence.

Practice Question 2: Identify the Relationship

A company consists of departments that are dissolved when the company shuts down. What relationship exists?

Answer: This is a composition. Departments cannot exist without the company.

Exercise 3: Deriving Tests from OCL

A Booking class has a date attribute. Write an OCL invariant and a corresponding test case.

Solution: Invariant: context Booking inv: self.date <= today. Test case: A booking with a future date is rejected as invalid.

Topic Analysis

  • Technical core: Diagram types, relationships, and cardinalities. UML provides a unified notation for both structural and dynamic design. Correct relationships and multiplicities form the backbone of a valid model.
  • Implementation challenges: Consistency across different diagrams. A class diagram must align with the messages in a sequence diagram and the states in a state machine diagram. Inconsistencies lead to misunderstandings and faulty implementation.
  • Security implications: OCL invariants as early validation. Formal constraints help catch invalid states before they occur and can be transformed into automated tests.
  • Documentation requirements: Version models and link them to requirements. UML diagrams are part of project documentation. They must be traceable, versioned, and linked to requirements and test cases.
  • Cost-benefit analysis: Modeling effort versus error prevention. UML requires time and ramp-up, but prevents costly mistakes through better early communication and planning.

Key References

  1. https://www.omg.org/spec/UML
  2. https://martinfowler.com/books/uml.html

FAQ: UML Fundamentals, Diagram Types, and Notation

1. What is UML?

UML stands for Unified Modeling Language. It is a standardized graphical modeling language used to describe, plan, and document software systems and business processes.

2. What are the main categories of UML diagrams?

UML diagrams are divided into structure diagrams and behavior diagrams. Structure diagrams show static composition, while behavior diagrams show dynamic flow.

3. What is a class diagram?

A class diagram is a structure diagram that depicts classes, their attributes, operations, and relationships to one another. It forms the static foundation of a system.

4. What is a sequence diagram?

A sequence diagram is a behavior diagram that shows the temporal flow of messages between objects. Lifelines represent objects, and arrows represent messages.

5. What is an activity diagram?

An activity diagram models workflows, processes, and decisions. It shows activities, branches, parallelization, and the flow from one activity to the next.

6. What is a state machine diagram?

A state machine diagram shows the states an object can assume throughout its lifecycle, along with the events and conditions that trigger state transitions.

7. What is an association?

An association is a loose relationship between two classes. It indicates that objects of the participating classes are connected without their lifecycles being dependent on each other.

8. What is aggregation?

Aggregation is a “part-of” relationship where the parts can exist independently of the whole. It is shown with an empty diamond on the side of the whole.

9. What is composition?

Composition is a strong “part-of” relationship where the parts cannot exist without the whole. It is shown with a filled diamond on the side of the whole.

10. What is generalization?

Generalization describes an inheritance relationship. A specialized class inherits attributes and operations from a more general class and can extend or override them.

11. What do multiplicities mean?

Multiplicities specify how many objects of one class can be related to how many objects of another class. Common notations are 1, 0..1, 1..*, and *.

12. What is OCL?

OCL stands for Object Constraint Language. It is a formal language used to precisely specify constraints, invariants, and preconditions and postconditions for UML models.

13. What are OCL invariants used for?

OCL invariants formulate conditions that must hold at all times. Test cases can be derived from them to verify system correctness.

14. What is a package diagram?

A package diagram groups model elements into packages and shows their dependencies. It helps organize large systems clearly and define namespaces.

15. What is a component diagram?

A component diagram shows software components, their interfaces, and dependencies. It is commonly used for architecture modeling and planning larger systems.

16. What is a use case diagram?

A use case diagram shows system functionality from the perspective of actors. It models which roles interact with the system and which use cases they trigger.

17. What is a lifeline?

A lifeline is a dashed line in a sequence diagram representing the existence of an object or role during an interaction. It serves as the basis for message exchange.

18. What is a guard?

A guard is a condition in square brackets on a state transition. The transition is executed only if the condition is true, for example [amount > 0].

19. What is a stereotype?

A stereotype extends a UML element with a specific meaning. It is written in double angle brackets, such as <<interface>> or <<abstract>>.

20. What is the difference between synchronous and asynchronous messages?

A synchronous message blocks the sender until the receiver finishes processing. An asynchronous message allows the sender to continue immediately while the receiver processes the message later.

21. What is a structure diagram?

A structure diagram shows the static composition of a system: classes, objects, components, packages, and their relationships. Examples include class diagrams, component diagrams, and package diagrams.

22. What is a behavior diagram?

A behavior diagram describes the dynamic flow of a system. It encompasses activity, sequence, state machine, and use case diagrams.

23. What does visibility mean in UML?

Visibility controls access to attributes and operations. + denotes public, - denotes private, # denotes protected, and ~ denotes package visibility. It affects encapsulation and reusability.

24. Why should UML models be versioned?

UML models evolve throughout a project. Versioning enables traceability of earlier states, allows review of changes, and ensures consistency with requirements and tests.

25. How can tests be derived from UML models?

Test cases can be developed from OCL invariants, preconditions and postconditions, and from defined relationships and cardinalities. For example, a test can verify that composition actually removes parts when the whole is deleted.

Continue Your UML Learning Path

The next article in the UML learning path covers Class Diagrams: Classes, Attributes, Methods, Inheritance, Association, Aggregation, Composition — detailed exploration of class diagrams and their relationships.

Back to Blog
Share:

Related Posts