Class Diagrams - 2
Aggregation
Aggregation is a way of saying that an object is part of anther class:
This is opposed to an is a relation with inheritance.
classDiagram
direction LR
HonoursCourse "1..*" o-- "6..*" Module
The aggregation also allows the Module
to be involved in multiple HonoursCourses
.
Composition
This is a specialised form of aggregation. In composition the whole strongly owns its parts:
- If the whole object is copied or deleted, its parts are copied or deleted with it.
- Multiplicity at the whole end must be
1
or0..1
.
classDiagram
direction LR
Car "1" *-- "4" Wheel
One wheel can’t be part of multiple cars.
Roles
Roles can be written on the ends of the arrows like so:
classDiagram
DirectorOfStudies "DoS" -- "directee" Student
Association with No Navigability
You can have associations with no relation type (arrows) but only a cardinality:
classDiagram
Student "1..*" -- "6" Module:is taking
Navigability
We can put an arrow on one, or both ends of the association to represent that it is possible for messages to be send in the direction of the arrow:
classDiagram
Student "1..*" <-- "6" Module:is taking
This arrow head just means that they are associated.
Qualified Composition
This is putting extra attributes on the association.
I can’t make this with either of my graphing tools and I’m pretty sure no-one ever uses this. Just put the attribute in the class like a normal person.
Derived Associations
Derived associations show the associations that are implied by others. They are denoted by a /
:
The direction of an association label is denoted by the black arrow.
Constraints
Constraints are represented as a dotted line between associations with a boolean operand. The two associations is then constrained by that operand.
I can’t make this with either of my graphing tools again. It can also be made using other methods.
Association Class
This is a class with a dotted line, connected to an association. It holds attributes specific to an association.
I can’t make this with either of my graphing tools again. It should be avoided and the following method used instead.
You can avoid using these by making an additional class normally:
classDiagram
Student "1..*" -- "6" Module: is taking
Student "1" -- "6" Mark
Mark: int mark
Mark "1..*" -- "1" Module
Interfaces
Interfaces specify operations that are visible outside of the class. All elements of an interface are public:
classDiagram
class Dog {
<<interface>>
pat() Reaction
}
They don’t require an implementation.
Abstract Classes
These are similar to interfaces but are general classes used to define a set of classes:
classDiagram
class Dog {
<<abstract>>
-int numberOfPats
-bool happy
+pat() Reaction
}