Design Pattern - UML


Association Relationships

  • Association is a link between classes representing a relationship between two classes.
  • It includes Multiplicity, indicating the relationship between objects of one class to another.
  • Direction specifies if the relationship is unidirectional or bidirectional.
  • Roles define the part each class plays in the relationship.
  • Names give a label to the relationship.
  • Constraints specify conditions within the relationship.

Example of Association in Java

public class LoginForm {
    private JButton loginButton;
}

Mermaid Diagram

classDiagram
    class LoginForm {
        -JButton loginButton
    }
    LoginForm --> JButton : contains

Bidirectional Association

Objects of two classes have references to each other.

Example in Java

public class Customer {
    private Product[] products;
}

public class Product {
    private Customer customer;
}

Mermaid Diagram

classDiagram
    class Customer {
        -Product[] products
    }
    class Product {
        -Customer customer
    }
    Customer --> Product : has
    Product --> Customer : has

Unidirectional Association

Only one class has a reference to the other, not reciprocated.

Self-Association

An object of a class contains a reference to another object of the same class.

Example in Java

public class Node {
    private Node subNode;
}

Mermaid Diagram

classDiagram
    Node --> Node : contains

Multiplicity Types

  • 1:1 (One to One)
  • 1:N (One to Many)
  • N:1 (Many to One)
  • N:M (Many to Many)

Aggregation Relationship

Represents a “whole-part” relationship, indicating that one class is a part of another class.

Example in Java

public class Car {
    private Engine engine;

    public Car(Engine engine) {
        this.engine = engine;
    }

    public void setEngine(Engine engine) {
        this.engine = engine;
    }
}

Mermaid Diagram

classDiagram
    class Car {
        -Engine engine
    }
    Car o--> Engine : contains

Composition Relationship

A strong form of aggregation where parts cannot exist without the whole.

Example in Java

public class Head {
    private Mouth mouth;

    public Head(Mouth mouth) {
        this.mouth = mouth;
    }
}

Mermaid Diagram

classDiagram
    class Head {
        -Mouth mouth
    }
    Head *--> Mouth : contains

Dependency Relationship

Shows that one class depends on another, indicating a use or association that is less permanent than other relationships.

Example in Java

public class Driver {

    public Driver(Car car) {
        car.move();
    }
}

public class Car {

    public void move() {
        System.out.println("Car is moving");
    }
}

Mermaid Diagram

classDiagram
    class Driver {
        +Driver(Car car) void
    }
    class Car {
        +move() void
    }
    Driver ..> Car : depends

Generalization Relationship

Represents inheritance where one class is a specialized version of another class.

Example in Java

public class Person {
    protected String name;
    protected int age;

    public void move() {
        System.out.println("Person is moving");
    }

    public void eat() {
        System.out.println("Person is eating");
    }
}

public class Student extends Person {
    private String studentId;

    public void study() {
        System.out.println("Student is studying");
    }
}

public class Teacher extends Person {
    private String teacherId;

    public void teach() {
        System.out.println("Teacher is teaching");
    }
}

Mermaid Diagram

classDiagram
    class Person {
        #String name
        #int age
        +move() void
        +eat() void
    }
    class Student {
        -String studentId
        +study() void
    }
    class Teacher {
        -String teacherId
        +teach() void
    }
    Student --|> Person : generalizes
    Teacher --|> Person : generalizes

Realization Relationship

Indicates that a class implements an interface, showing a contract that the class agrees to perform.

Example in Java

public interface Vehicle {
    void move();
}

public class Ship implements Vehicle {

    @Override
    public void move() {
        System.out.println("Ship is moving");
    }
}

public class Car implements

 Vehicle {

    @Override
    public void move() {
        System.out.println("Car is moving");
    }
}

Mermaid Diagram

classDiagram
    class Vehicle {
        +move() void
    }
    <<interface>> Vehicle
    class Ship {
        +move() void
    }
    class Car {
        +move() void
    }
    Ship ..|> Vehicle : realizes
    Car ..|> Vehicle : realizes