Class Diagrams

A class diagram is used to represent the structure of a system. It's mostly used for object-oriented modeling.

class.pu

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.

attribute

  • Derived attributes

Derived attributes are attributes whose values are calculated using other attributes. They are usually created for convenience.

derived attribute UML

  • Composite attributes

Composite attributes are attributes composed of multiple attributes.

composite attribute UML

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>>.

constructor


Associations and multiplicity

Associations represent which classes are linked to which other classes.

association_1Β Β Β  association_2

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.

association

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.

assos_class

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

generalization

⚠️ 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.

abstract

πŸ’‘ 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.

interface

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.

interface lollipop


Packages

Packages are used to group and sort classes.

system


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.

composite class UML

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.

composite class UML