Class Diagrams
A class diagram is used to represent the structure of a system. It's mostly used for object-oriented modeling.
It's abstract and the concrete version is called an object diagram. Creating an object from a class is called instantiation.
A class π‘ is an abstract representation of a concept, such as a Book or a Person. Each class has:
- attributes π«: these are the data properties of a class. For instance, a title.
- operations π: these are the behaviors that concepts can perform. For instance, from a book, we can get its details (using getDetails).
There are also associations π£οΈ which represent the relationships between concepts (e.g., classes). There are none in the example.
Basics syntax
We use "member" to refer to both attributes and operations.
Modifiers
Each member can have a modifier (explicit or not) which is something to limit who can access an attribute or an operation.
-
+
is for public -
-
is for private -
~
is for package -
#
is for protected
Class Members
Class members (a.k.a. static
members) are attributes and methods that are shared across every instance.
They are commonly used for constant attributes, utilities...
In UML, such members are underlined π΅.
Types
Natively, there are not many UML types so most are adding their own types based on what types are defined in the language they target.
-
void
is used for "nothing" -
int
is used for "a number" -
float/real/double
is used for "a real" -
boolean
is used for "a boolean" (true/false) -
string
is used for "a text" - ...
Overview
Classes
Classes are made up of both attributes and methods, separated by a line. The name of the class must always start with an uppercase.
Attributes
Each attribute must have at least a name and a type. It may have a modifier and a default value.
You can have OCL constraints such as {final}
right after the attribute.
- Derived attributes
Derived attributes are attributes whose values are calculated using other attributes. They are usually created for convenience.
- Composite attributes
Composite attributes are attributes composed of multiple attributes.
Operations
Each attribute must have at least a name followed by parenthesis. It may have a modifier, parameters (comma-separated), and a return type.
β οΈ No return type is NOT the same as void
.
π Methods can be annotated: <<constructor>>
or <<destroy>>
.
Associations and multiplicity
Associations represent which classes are linked to which other classes.
Β Β Β
Associations do not necessarily represent attributes while most do.
- There could be multiple associations between two classes
- You can add arrows if the association is unidirectional. By default, there is no orientation meaning it's bidirectional.
- An association from the class to itself is called self-association
Multiplicity
The multiplicity indicates how many instances of a class are associated with instances of another class.
- There are a..b instances of B associated with A.
- There are c..d instances of A associated with B.
List of possible values
- n: same as n..n
- *: same as 0..*
- 0..1: 0 or 1
- 0..*: same as *
- 1..*: 1 or more
- n..*: n or more
- n..m: at least n, and up to m
- n..n: exactly n
For instance, we could replace a..b with 0..1 (meaning 0 or 1) or with * (0 or more).
Association classes
Association classes are used to represent relationships with additional attributes. The name of the class is the same as the relationship.
For instance, Class1
could be a Student, Class2
a course, and we could have an association class Enrollment
linking the two.
Constraints on associations
It's possible to add constraints to associations, such as two associations being mutually exclusive (e.g. can't have both).
Link two associations or more with a dotted line and add constraints on it that will be applied to all linked associations.
-
Inclusion
{IN}/{SUBSET}/{I}
: either all associations exist or none -
Exclusion
{X}
: only one may exist -
Equality/Simultaneity
{=}, {AND}, {S}
: all associations must exist -
Total/Coverage/Inclusive
{T}, {OR}
: at least 1 association exist -
Partition/Exclusive
{XT}, {P}, {+}
: only 1 association exist
For the inclusion constraint, we use an arrow instead of a dotted line. If the arrow goes from X to Y, it means that X can only exist if Y exists.
π‘ You can also use this syntax to write dynamic constraints, such as {we can't buy before ordering}
.
Inheritance
Generalization
A generalization is used to factorize attributes and methods in a "parent" class. Given a class B generalizing A, the class B will have:
-
public
members in A -
protected
members in A - members declared in B
β οΈ Commonly, a child/class can only have one parent/super class.
Abstract classes
An abstract class is a normal class aside from the fact that we are allowed to have <<abstract>>
methods. It means that such a class won't write the code for the method and declare that its subclass will.
π‘ Classes generalizing an abstract class are abstract too unless they implement abstract methods.
π‘ Use italic and/or <<abstract>>
to mark abstract methods/classes.
π£οΈ It's quite used for generic code. We would create a method: eat(food: Food)
with Food
being an abstract class. Any instance of a class implementing food can be used with eat
.
Interfaces
There is a limit to abstract classes as we can only inherit from one class (in most languages). Interfaces are classes only with abstract public methods. You can realize ("inherit") multiple interfaces.
β‘οΈ Interfaces can actually have more than methods, such as class members
, concrete methods
(<<default>>
keyword) and private methods
.
Machine is an interface implemented by ATM
and used by Client
.
There is an alternative design called lollipop representation. Interface1
is implemented by Class1
and used by Class2
.
Packages
Packages are used to group and sort classes.
Special associations
Compositions
Composition is a special association for a 1
to n..m
relationship where a "part" class from n..m
side is managed (created, used, destroyed) by the other "composite" class. The part can't exist without the composite.
Aggregations
An aggregation is a special association for a 1
to n..m
relationship where a "aggregator" class from n..m
side has a superior relationship over "aggregated" classes.