Skip to content
IRC-CodingIRC-Coding
UML DiagramsClass DiagramSequence DiagramActivity DiagramUse Case Diagram

UML Diagrams: Class, Sequence & Use Case

Master UML notation for software modeling. Learn class, sequence, activity, and use case diagrams with relationships and practical examples.

S

schutzgeist

5 min read
UML Diagrams: Class, Sequence & Use Case

UML Diagrams: Class, Sequence, Activity & Use Case Diagrams

This guide provides a comprehensive overview of UML diagrams – covering all the major diagram types, relationships, and practical examples.

In a Nutshell

UML (Unified Modeling Language) is the standardized graphical notation for modeling software systems, offering various diagram types to represent different aspects of software development.

Technical Definition

Unified Modeling Language (UML) is a modeling language standardized by the Object Management Group (OMG) for object-oriented software development.

Main diagram categories:

  • Structural diagrams: Static aspects (classes, components, deployment)
  • Behavioral diagrams: Dynamic aspects (activities, sequences, states)
  • Interaction diagrams: Communication between objects

Most important diagrams:

  • Class diagram: Classes, attributes, methods, and relationships
  • Sequence diagram: Temporal flows and message passing
  • Activity diagram: Process flows and decision points
  • Use case diagram: Requirements and actor interactions

UML serves as a communication tool between developers, architects, and stakeholders, and forms the basis for code generation and documentation.

Key Points for Exam Preparation

  • Class diagram: Static structure with attributes, methods, relationships
  • Sequence diagram: Temporal interactions between objects
  • Activity diagram: Process flows with decisions and parallelism
  • Use case diagram: Functional requirements and actor interactions
  • Relationships: Association, aggregation, composition, inheritance
  • Visibility modifiers: public (+), private (−), protected (#), package (~)
  • Multiplicities: 1, , 0..1, 1.., 0..*
  • Relevant for software architecture and design certification exams

Core Components

Class Diagram

  1. Classes: Rectangles containing name, attributes, methods
  2. Attributes: Properties with type and visibility
  3. Methods: Operations with parameters and return type
  4. Relationships: Connections between classes

Sequence Diagram

  1. Actors: Participants in the interaction
  2. Lifelines: Vertical lines representing objects
  3. Messages: Horizontal arrows between lifelines
  4. Activation boxes: Rectangles on lifelines

Activity Diagram

  1. Actions: Rounded rectangles
  2. Decision points: Diamonds for branching
  3. Start/End: Circles for process start and end
  4. Synchronization bars: For parallel flows

Use Case Diagram

  1. Use cases: Ovals representing functionalities
  2. Actors: Stick figures for users or roles
  3. System boundary: Rectangle surrounding use cases
  4. Relationships: Lines between actors and use cases

Practical Examples

1. Class Diagram (E-Commerce System)

@startuml ECommerceClassDiagram

class Kunde {
  -kundenId: Long
  -name: String
  -email: String
  -adresse: String
  +getKundenId(): Long
  +bestellen(produktId: Long, menge: Int): Bestellung
  +getBestellungen(): List<Bestellung>
}

class Bestellung {
  -bestellId: Long
  -bestelldatum: Date
  -gesamtbetrag: Decimal
  -status: String
  +berechneGesamtbetrag(): Decimal
  +setStatus(status: String): void
  +getPositionen(): List<Bestellposition>
}

class Produkt {
  -produktId: Long
  -name: String
  -preis: Decimal
  -lagerbestand: Int
  +getPreis(): Decimal
  +pruefeVerfuegbarkeit(menge: Int): Boolean
  +reduziereLagerbestand(menge: Int): void
}

class Bestellposition {
  -positionsId: Long
  -menge: Int
  -einzelpreis: Decimal
  +berechneGesamtpreis(): Decimal
}

' Beziehungen
Kunde "1" -- "0..*" Bestellung : erstellt >
Bestellung "1" -- "1..*" Bestellposition : enthält >
Bestellung "1" -- "*" Produkt : bezieht sich auf >
Produkt "0..*" -- "0..*" Bestellposition : wird bestellt >

@enduml

2. Sequence Diagram (Order Process)

@startuml BestellSequenzDiagramm

actor Kunde
participant "BestellService" as Service
participant "ProduktService" as Produkt
participant "ZahlungsService" as Zahlung
participant "LagerService" as Lager

Kunde -> Service: bestelleProdukte(produkte, menge)
activate Service

Service -> Produkt: pruefeProdukte(produkte)
activate Produkt
Produkt --> Service: verfuegbarkeitsInfo
deactivate Produkt

alt alle Produkte verfügbar
    Service -> Lager: reserviereLager(produkte, menge)
    activate Lager
    Lager --> Service: reservierungsBestaetigung
    deactivate Lager
    
    Service -> Zahlung: verarbeiteZahlung(kunde, betrag)
    activate Zahlung
    Zahlung --> Service: zahlungsBestaetigung
    deactivate Zahlung
    
    Service -> Lager: bestätigeReservierung(reservierungsId)
    activate Lager
    Lager --> Service: bestätigungErfolgt
    deactivate Lager
    
    Service --> Kunde: bestellBestaetigung(bestellId)
else Produkte nicht verfügbar
    Service --> Kunde: verfuegbarkeitsFehlerr(produkte)
end

deactivate Service

@enduml

3. Activity Diagram (Order Processing)

@startuml BestellAktivitaetsDiagramm

start

:Bestellung eingegangen;

if (Produkt verfügbar?) then (ja)
  :Lagerbestand prüfen;
  
  fork
    :Zahlung verarbeiten;
  fork again
    :Lieferadresse prüfen;
  end fork
  
  if (Zahlung erfolgreich?) then (ja)
    :Bestellung bestätigen;
    :Lagerbestand reduzieren;
    :Versand veranlassen;
    stop
  else (nein)
    :Bestellung ablehnen;
    :Lagerreservierung aufheben;
    stop
  endif
else (nein)
  :Verfügbarkeitsfehler melden;
  :Alternative Produkte vorschlagen;
  stop
endif

@enduml

4. Use Case Diagram (E-Commerce System)

@startuml ECommerceUseCaseDiagram

actor Kunde
actor Administrator
actor Lieferant

rectangle "E-Commerce System" {
  usecase "Produkt suchen" as UC1
  usecase "Produkt ansehen" as UC2
  usecase "Warenkorb verwalten" as UC3
  usecase "Bestellung aufgeben" as UC4
  usecase "Bestellung verfolgen" as UC5
  usecase "Bewertung abgeben" as UC6
  usecase "Produkt verwalten" as UC7
  usecase "Bestellungen bearbeiten" as UC8
  usecase "Lieferung verwalten" as UC9
}

' Beziehungen
Kunde --> UC1
Kunde --> UC2
Kunde --> UC3
Kunde --> UC4
Kunde --> UC5
Kunde --> UC6

Administrator --> UC7
Administrator --> UC8

Lieferant --> UC9

' Include-Beziehungen
UC4 --> UC3 : <<include>>
UC4 --> UC2 : <<include>>

' Extend-Beziehungen
UC6 --> UC4 : <<extend>>

@enduml

5. Java Code Generated from Class Diagram

// Kunde.java
public class Kunde {
    private Long kundenId;
    private String name;
    private String email;
    private String adresse;
    private List<Bestellung> bestellungen = new ArrayList<>();
    
    public Long getKundenId() {
        return kundenId;
    }
    
    public Bestellung bestellen(Long produktId, int menge) {
        Bestellung bestellung = new Bestellung();
        bestellung.setBestelldatum(new Date());
        // Bestellposition hinzufügen
        Bestellposition position = new Bestellposition();
        position.setMenge(menge);
        bestellung.addPosition(position);
        
        bestellungen.add(bestellung);
        return bestellung;
    }
    
    public List<Bestellung> getBestellungen() {
        return new ArrayList<>(bestellungen);
    }
}

// Bestellung.java
public class Bestellung {
    private Long bestellId;
    private Date bestelldatum;
    private BigDecimal gesamtbetrag;
    private String status;
    private List<Bestellposition> positionen = new ArrayList<>();
    
    public BigDecimal berechneGesamtbetrag() {
        return positionen.stream()
            .map(Bestellposition::berechneGesamtpreis)
            .reduce(BigDecimal.ZERO, BigDecimal::add);
    }
    
    public void setStatus(String status) {
        this.status = status;
    }
    
    public List<Bestellposition> getPositionen() {
        return new ArrayList<>(positionen);
    }
    
    public void addPosition(Bestellposition position) {
        positionen.add(position);
    }
}

// Produkt.java
public class Produkt {
    private Long produktId;
    private String name;
    private BigDecimal preis;
    private int lagerbestand;
    
    public BigDecimal getPreis() {
        return preis;
    }
    
    public boolean pruefeVerfuegbarkeit(int menge) {
        return lagerbestand >= menge;
    }
    
    public void reduziereLagerbestand(int menge) {
        if (pruefeVerfuegbarkeit(menge)) {
            lagerbestand -= menge;
        } else {
            throw new IllegalArgumentException("Nicht genügend Lagerbestand");
        }
    }
}

// Bestellposition.java
public class Bestellposition {
    private Long positionsId;
    private int menge;
    private BigDecimal einzelpreis;
    
    public BigDecimal berechneGesamtpreis() {
        return einzelpreis.multiply(new BigDecimal(menge));
    }
}

UML Relationships in Detail

Association

Kunde 1..* --* Bestellung
  • Both classes depend on each other
  • Lifespans are independent

Aggregation

Fahrzeug 1 --* Reifen
  • Tires can exist without a vehicle
  • “has-a” relationship

Composition

Auto 1 --* Motor
  • Motor exists only with the car
  • “is-part-of” relationship

Inheritance

Fahrzeug <|-- Auto
  • Auto inherits from Fahrzeug
  • “is-a” relationship

Implementation

Interface <|.. Klasse
  • Class implements interface
  • Realization relationship

Multiplicities

SymbolMeaningExample
1Exactly one1 customer
0..1Zero or one0..1 address
*Zero or more* orders
1..*At least one1..* line items
0..*Zero to many0..* products
2..4Between 2 and 42..4 wheels

Visibility Modifiers

SymbolMeaningDescription
+publicAccessible from anywhere
privateAccessible only within the class
#protectedAccessible within class and subclasses
~packageAccessible within the package

Advantages and Disadvantages

Advantages of UML

  • Standardization: Unified notation for everyone
  • Visualization: Complex relationships become clear
  • Communication: Shared language for the team
  • Documentation: Can be generated automatically
  • Code generation: Direct implementation into code

Disadvantages

  • Complexity: Large systems can become unwieldy
  • Learning curve: Requires training and practice
  • Over-engineering: Risk of excessive detail
  • Maintenance: Changes require diagram updates

Common Exam Questions

  1. What is the difference between aggregation and composition? Aggregation: “has-a” (parts can exist independently), composition: “is-part-of” (parts exist only with the whole).

  2. Explain the multiplicities 1.. and 0..1!* 1..*: At least one, unlimited. 0..1: Zero or exactly one.

  3. When do you use sequence diagrams? To represent temporal flows and interactions between objects.

  4. What is the purpose of use case diagrams? To model requirements from the perspective of users or actors.

Key Resources

  1. https://www.uml.org/
  2. https://de.wikipedia.org/wiki/Unified_Modeling_Language
  3. https://plantuml.com/
Back to Blog
Share:

Nächster Artikel in Software Architecture

Weiterlesen
3-Tier Architecture Explained: UI, Business, Data

Related Posts