Software Engineering
UML Relationships with PlantUml
Explore basic examples
UML - hierarchy of relationships
Dependence - general relationship
Every relationship is a dependency.
Definition
The source element depends on the target element and may be affected by changes to it
UML syntax source to target
- - - >
PlantUml
@startuml
title Dependence
class Order
class Account
Order ..> Account
@enduml
Code example
class Order {
}
class Account {
}
Asosjacja
Definition
The description of a set of links between objects
UML syntax source to target
--->
or ---
(bidirectional)
PlantUml
@startuml plantuml
title Association
class Order
class Account
Order --> Account
@enduml
Code example
class Order {
void checkAccount(Account account); //not necessarily as parameter
}
class Account {
}
The Order class uses the Account class. Order needs access to account.
Therefore, changes made in Account can lead to errors in the Order class.
If the Order class knows nothing about the Account class, then changes in the Account class don't matter to it.
Aggregation
Definition
The target element is a part of the source element
UML syntax source to target
<>--->
<>---
(empty diamond)
PlantUml
@startuml
title Aggregation
class Order
class Account
Order O-- Account
@enduml
Code example
class Order {
List<Account> accounts;
void checkAccounts();
}
class Account {
public String accountName();
}
The Order class contains objects of the Account class, and the Account class can exist independently.
Composition
Definition
A strong (more constrained) form of aggregation
UML syntax source to target
<+>--->
(filled diamond)
PlantUml
@startuml
title Composition
class Order
class Account
Order *-- Account
@enduml
Code example
class Order {
List<Account> accounts;
createAccount(Strig name);
}
class Account {
}
The Order class contains objects of the Account class, and the Account class can NOT exist independently because it relies on being part of the Order class.
Realization (implementation)
Definition
The source element guarantees to carry out the contract specified by the target element
UML syntax source to target
- - - |>
Dotted Line with Diamond Arrowhead
PlantUml
@startuml
title Realization
class Writable
class CsvWritable
CsvWritable --|> Writable
@enduml
Code example
class Writable implements CsvWritable {}
The CsvWritable class implements the Writable interface.
Generalization (inheritance)
Definition
The source element is a specialization of the more general target element and may be substituted for it
UML syntax source to target
---|>
arrow with full diamond
PlantUml
@startuml
title Generalization
class Child
class Parent
Child --|> Parent
@enduml
Code example
class Child extends Parent {}
The Child class inherits from the Parent class.
Dependency stereotype
Use
<<use>>
The most common dependency stereotype is «use», which simply states that the client makes use of the supplier in some way. If you see just a dashed dependency arrow with no stereotype, then you can be pretty sure that «use» is intended.
PlantUML
@startuml
'https://plantuml.com/sequence-diagram
title use stereotype
class Client {
check(s:Supplier)
get():Supplier
doSomething()
}
class Supplier
Client ..> Supplier :<<use>>
@enduml
Source
- UML 2 and the Unified Process Jim Arlow
- Core Java Volume 1 - Fundamentals (11th Edition)