A Class Diagram is one of the most commonly used UML diagrams in software design. It provides a static view of a system by depicting its classes, attributes, operations, and the relationships among objects. Class diagrams are vital for object-oriented modeling, as they define the blueprint of a system’s structure.
A class represents a blueprint for objects in the system. It is defined by:
- Name: The name of the class (mandatory).
- Attributes: The data fields of the class.
- Methods (Operations): The functionality or behavior of the class.
+----------------+
| ClassName |
+----------------+
| - attribute1 |
| - attribute2 |
+----------------+
| + method1() |
| + method2() |
+----------------+
- Visibility Modifiers:
+
Public#
Protected-
Private
- Static Fields are underlined.
mTotalEmployeeCount
,getInstance()
+------------------------+
| Employee |
+------------------------+
| - name: String |
| - id: int |
+------------------------+
| + getName(): String |
| + setId(id: int): void |
+------------------------+
Interfaces (Either in Italics or <<>> brackets)
+-------------------------------+
| <<ClickListenerInterface>> |
+-------------------------------+
+-------------------------------+
| + onClick(): void |
+-------------------------------+
Relationships define how classes are connected. Key relationships in class diagrams include:
- Represents a relationship between two classes.
- Example: An Employee works in a Department.
Notation: Solid line between classes. (e.g Employee ―――> Department
)
- A specialized association that represents a "whole-part" or "has-a" relationship.
- The part can exist independently of the whole.
Notation: Solid line with a hollow diamond near the whole. (Person ―――◇ House
, both Person & House can exist independently)
- A stronger form of aggregation where the part cannot exist without the whole.
Notation: Solid line with a filled diamond near the whole. (RC ―――◆ Vechicle
, Nose ―――◆ Human
)
- Represents an "is-a" relationship.
- Example: A Manager is an Employee.
Notation: Solid line with a hollow arrow pointing to the parent class. (Employee ――(extends)――|> Manager
)
- Represents a "uses-a" relationship where one class depends on another.
- Mostly for implementational uses.
Notation: Dotted line with an open arrow. (--(implements)--|>
or Button --(uses a)--|> ClickListener
)
Here is an example of a class diagram for a simple employee management system:
+-----------------------------------+
| Employee |
+-----------------------------------+
| - name: String |
| - id: int |
| - salary: double |
+-----------------------------------+
| + getName(): String |
| + setSalary(salary: double): void |
+-----------------------------------+
▲
|(extends)
+----------------------+
| Manager |
+----------------------+
| - department: String |
+----------------------+
| + assignTask(): void |
+----------------------+
+-----------------+ +-----------------+
| Department |<――――――| Employee |
+-----------------+ +-----------------+
| - name: String | | |
| - id: int | | |
+-----------------+ +-----------------+
- Identify Classes:
- Analyze the requirements to identify classes that represent real-world entities.
- Define Attributes and Methods:
- Specify the data and behaviors for each class.
- Establish Relationships:
- Determine how the classes interact and connect.
- Use a Modeling Tool:
- Use tools like StarUML, Lucidchart, or Microsoft Visio to create the diagram.
- Iterate and Review:
- Refine the diagram based on feedback and ensure it aligns with the system’s requirements.
- StarUML: A popular open-source UML tool.
- Lucidchart: A web-based diagramming tool.
- Microsoft Visio: A comprehensive diagramming tool.
- Draw.io: A free, web-based tool for creating diagrams.
Vehicle
: A base class representing general vehicle attributes (engine
,wheel
,fuel
, andvehicleColor
). It is extended by specific types of vehicles.MotoCycle and Car
: Subclasses extending theVehicle
class. These inherit attributes and methods fromVehicle
.VehicleRC
: Associated withCar
orMotocycle
as part of a composition relationship (i.e, the RC details cannot exist without car).Gender
: An enumeration that categorizes gender values (e.g.,Male
,Female
,Other
).Person
: A base class representing general details likename
,age
, andgender
. It contains methods for retrieving personal data.Employee
: A subclass ofPerson
, adding specific attributes likeid
,jobType
, and aVehicle
(showing aggregation). Employees can have vehicles linked to them, but vehicles can exist independently.Manager
: A subclass ofEmployee
, inheriting all its properties and methods.Department
: Represents organizational units with attributes likeid
andname
. It has aone-to-many
association withEmployee
, indicating a department can have multiple employees.EmployeeChangeEvent
: Shows an interface used byDepartment
to manage events likeonEmployeeAdded
andonEmployeeRemoved
.
MotoCycle
andCar
inherit fromVehicle
.Employee
andManager
inherit fromPerson
.
Department
has aone-to-many
association withEmployee
(indicated by1
andmany
).Person
has adependency
relationship withGender
.
MotorCycle
has a aggregaring relationship withEmployee
. (indicated by a hollow diamond towhole
class).
VehicleRC
has a composition relationship withVehicle
(solid diamond withwhole
class), meaning a registration details cannot exist without vehicle.
Department
depends on theEmployeeChangeEvent
interface to trigger employee-related events.
- Solid lines: Represent associations.
- Hollow diamonds: Represent aggregation.
- Solid diamonds: Represent composition.
- Hollow arrows: Represent inheritance (generalization).
- Dotted lines with open arrows: Represent dependencies.
- Enumeration (Gender): A special class type with predefined constant values.
The above UML image is generate from PlantUML plugin in android studio. Here is the below code for cited image
@startuml
'https://plantuml.com/sequence-diagram
enum Gender {
+ Male: Int = 0
+ Female: Int = 1
+ Other: Int = 2
}
abstract Person {
- name: String
- age: Int
- gender: Gender
+ getName(): String
+ getAge(): String
+ getGender(): Int
+ {abstract} getJobDescription(): String
}
Gender "1" <-- "1" Person : Uses
class Employee {
- id : Int
- gender: Gender
- vehicle: Vehicle
- jobType: String
+ {static} LOG_EVENT_ID: Int
+ Employee(id: Int, name: String, age: Int, gender: Gender)
super(name, age, gender)
+ getId(): Int
+ setJob(type: String):
+ getJob(): String
}
Person <|-- Employee : (extends)
class Manager {
}
Employee <|-- Manager : (extends)
class Department {
- id: Int
- name: String
- employees: List<Employee>
+ getEmployees(): List<Employee>
+ getEmployee(id: Int): Employee
}
Department "1" -- "many" Employee
interface EmployeeChangeEvent {
+ onEmployeeAdded(): Unit
+ onEmployeeRemoved(): Unit
}
Department ..> EmployeeChangeEvent : (uses a)
abstract class Vehicle {
+ engine: Engine
+ wheel: Wheel
+ fuel: Fuel
+ vehicleColor: Int
}
class MotoCycle {
}
Vehicle <|- MotoCycle : (extends)
MotoCycle --o Employee : (has a)
Class Car {
}
Vehicle <|-- Car : (extends)
Car --o Manager : (has a)
class VehicleRC {
+ vehicleId: Int
+ rcId: Int
}
Vehicle "1" *-- "1" VehicleRC
@enduml