UML

UML

The Unified Modeling Language (UML) is a standardized way of visualizing software design. It’s a good way to communicate your design to other people, and it’s also a good way to get a high-level overview of your code.

The most applicable type of UML diagram for software design is the class diagram. A class diagram shows the classes in your program, the relationships between those classes, and the methods and attributes of those classes. We don’t generally show constructors or simple getters and setters in class diagrams, and we tend to label aggregation and association relationships with the relevant field name.

Class Diagram Symbols #

Class:

class MyClass {
    ' The + stands for public
    + getMyAttribute(): int
    ' The - stands for private
    - myAttribute: int
    ' The # stands for protected
    # helperMethod(): void
}

Class diagram

Interface:

interface MyInterface {
    + myMethod(): void
}

Class diagram

Abstract class:

abstract class MyAbstractClass {
    + myMethod(): void
}

Class diagram

Class Diagram Relationships #

Dependency: One class uses another class, but the other class doesn’t use the first class. This is a weak relationship.

class PlaneTicketBuyer {
    + buyTicket(Ticket): void
}

class Ticket {
    - price: int
    + rescheule(): void
    - flight: Flight
}

PlaneTicketBuyer ..> Ticket

Class diagram

Aggregation: One class has one or more instances of another class, but the other class doesn’t know about the first class. This is a weak relationship.

class Seat {
    - seatNumber: int
    + isWindowSeat(): boolean
}

class Flight {
    + delay(time: int): void
}

Flight "1" o-- "1..*" Seat : seats <List>

Class diagram

Composition: One class has one or more instances of another class, and the instances of the other class wouldn’t exist without the first class. This is a strong relationship.

class Gate {
    - gateNumber: int
    + assignFlight(flight: Flight): void
}

class Airport {
    + delay(time: int): void
}

Airport "1" *-- "1..*" Gate : gates <List>

Class diagram

Inheritance: One class inherits from another class. This is a strong relationship.

class Seat {
    - seatNumber: int
    + isWindowSeat(): boolean
}

class FirstClassSeat extends Seat {
    - meal: Meal
    + closeShade(): void
}

Class diagram

Realization: One class implements an interface. This is a strong relationship.

interface Flyable {
    + fly(): void
}

class Plane implements Flyable {
    + fly(): void
}

Class diagram

Association: Two classes have a symmetrical aggregation relationship. This is a strong relationship.

class Flight {
    - gate: Gate
    + getGate(): Gate
}

class Gate {
    - flight: Flight
    + getFlight(): Flight
}

Flight "1" -- "1" Gate : > boards at

Class diagram

Cardinality and Multiplicity #

For aggregation, composition, and association relationships, you can specify the cardinality and multiplicity of the relationship. Cardinality is the number of instances of one class that can be associated with an instance of another class. Multiplicity helps us note the cardinality of a relationship.

Examples:

  • 1 means that there can be only one instance of the other class associated with an instance of the first class.
  • 1..* means that there can be one or more instances of the other class associated with an instance of the first class.
  • 0..* means that there can be zero or more instances of the other class associated with an instance of the first class.