UML Diagrams: Class, Sequence, Activity & UseCase Diagrams
This article is a comprehensive overview of UML diagrams – including all important diagram types, relationships, and practical examples.
In a Nutshell
UML (Unified Modeling Language) is the standardized graphical notation for modeling software systems, which provides various diagram types for different aspects of software development.
Compact Technical Description
Unified Modeling Language (UML) is a modeling language standardized by the Object Management Group (OMG) for object-oriented software development.
Main diagram types:
- Structure diagrams: Static aspects (classes, components, deployment)
- Behavior diagrams: Dynamic aspects (activities, sequences, states)
- Interaction diagrams: Communication between objects
Most important diagrams:
- Class diagram: Classes, attributes, methods, relationships
- Sequence diagram: Temporal sequences and message flow
- Activity diagram: Process flows and decisions
- UseCase 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.
Exam-Relevant Key Points
- Class diagram: Static structure with attributes, methods, relationships
- Sequence diagram: Temporal interactions between objects
- Activity diagram: Process flows with decisions and parallelism
- UseCase diagram: Functional requirements and actor interactions
- Relationships: Association, aggregation, composition, inheritance
- Visibility modifiers: public (+), private (-), protected (#), package (~)
- Multiplicities: 1, , 0..1, 1.., 0..*
- IHK-relevant for software architecture and design
Core Components
Class Diagram
- Classes: Rectangles with name, attributes, methods
- Attributes: Properties with type and visibility
- Methods: Operations with parameters and return type
- Relationships: Connections between classes
Sequence Diagram
- Actors: Participants of the interaction
- Lifelines: Vertical lines for objects
- Messages: Horizontal arrows between lifelines
- Activation boxes: Rectangles on lifelines
Activity Diagram
- Actions: Rounded rectangles
- Decisions: Diamonds for branching
- Start/End: Circles for process start/end
- Synchronization bars: For parallel flows
UseCase Diagram
- UseCases: Ovals for functionalities
- Actors: Stick figures for users/roles
- System boundaries: Rectangles around UseCases
- Relationships: Lines between actors and UseCases
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. UseCase 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
- Lifecycles are independent
Aggregation
Fahrzeug 1 --* Reifen
- Tires can exist without a vehicle
- “has-a” relationship
Composition
Auto 1 --* Motor
- Engine exists only with car
- “is-part-of” relationship
Inheritance
Fahrzeug <|-- Auto
- Auto inherits from Fahrzeug
- “is-a” relationship
Implementation
Interface <|.. Klasse
- Class implements interface
- Realization relationship
Multiplicities
| Symbol | Meaning | Example |
|---|---|---|
| 1 | Exactly one | 1 Customer |
| 0..1 | Zero or one | 0..1 Address |
| * | Zero or more | * Orders |
| 1..* | At least one | 1..* Positions |
| 0..* | Zero to any number | 0..* Products |
| 2..4 | Between 2 and 4 | 2..4 Wheels |
Visibility Modifiers
| Symbol | Meaning | Description |
|---|---|---|
| + | public | Accessible from everywhere |
| - | private | Accessible only within the class |
| # | protected | Accessible within class and subclasses |
| ~ | package | Accessible within the package |
Advantages and Disadvantages
Advantages of UML
- Standardization: Uniform notation for everyone
- Visualization: Complex relationships understandable
- Communication: Common language for the team
- Documentation: Automatic generation possible
- Code Generation: Direct implementation into code
Disadvantages
- Complexity: Confusing with very large systems
- Learning curve: Requires time to learn
- Over-Engineering: Too detailed modeling
- Maintenance: Changes require diagram updates
Common Exam Questions
-
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).
-
Explain the multiplicities 1.. and 0..1!* 1..*: At least one, any number. 0..1: Zero or exactly one.
-
When do you use sequence diagrams? To represent temporal sequences and interactions between objects.
-
What is the purpose of UseCase diagrams? To model requirements from the perspective of users/actors.