Object-Oriented Modeling & UML Class Diagrams
Learn to identify objects, define classes, and express relationships using UML
Objectives
By the end of this practical work, you will be able to:
- Identify objects and classes from a problem description
- Define attributes (properties) and methods (behaviors) for classes
- Determine relationships between classes (association, aggregation, composition)
- Create UML class diagrams using industry-standard notation
- Use Mermaid for creating class diagrams
Prerequisites
- Understanding of basic OOP concepts (from lecture)
- Access to a diagramming tool (recommendations below)
Recommended Tool: Mermaid
We'll use Mermaid for creating UML diagrams:
- Text-based - Write diagrams as code, easy to version control
- Live preview - Use the Mermaid Live Editor to build and test
- Widely supported - Works in GitHub, GitLab, Notion, and many IDEs
UML Class Diagram Refresher
Class Notation
A UML class is represented as a rectangle divided into three sections:
┌──────────────────────────┐
│ <<ClassName>> │ ← Class name (centered, bold)
├──────────────────────────┤
│ - privateAttribute: Type │ ← Attributes (properties)
│ + publicAttribute: Type │ - = private, + = public
│ # protectedAttribute │ # = protected
├──────────────────────────┤
│ + publicMethod(): Type │ ← Methods (behaviors)
│ - privateMethod(): void │
└──────────────────────────┘
Relationship Types
| Relationship | Symbol | Meaning | Example |
|---|---|---|---|
| Association | ─────── | "uses" or "knows about" | Student ─── Course |
| Aggregation | ◇─────── | "has" (weak ownership) | Team ◇─── Player |
| Composition | ◆─────── | "contains" (strong ownership) | House ◆─── Room |
| Inheritance | ───────▷ | "is a" (extends) | Dog ──▷ Animal |
| Implementation | - - - - -▷ | "implements" (interface) | Duck - -▷ Swimmable |
Multiplicity
Numbers near relationship lines indicate how many instances can be involved:
1- Exactly one0..1- Zero or one (optional)*or0..*- Zero or more1..*- One or moren- Exactly n (e.g., 3)
Car Rental System
~30 minProblem Description
A car rental company wants to manage their fleet and rentals. The system should track:
- Cars - Each car has a license plate, brand, model, color, daily rental rate, and availability status
- Customers - Customers have a name, driver's license number, phone, and email
- Rentals - A rental records which customer rented which car, the rental dates, and total cost
- Customers can have multiple rentals (history), but each rental is for one car
- A car can only be rented by one customer at a time
Your Task
- Identify the main classes
- Define attributes for each class (with appropriate types)
- Define key methods for each class
- Determine relationships and multiplicities
- Create a UML class diagram
Hints
- Think about what data each entity needs to store (attributes)
- Think about what actions each entity can perform (methods)
- Consider: Does a Rental "own" the Car and Customer, or just reference them?
Example Solution Structure (Mermaid)
classDiagram
class Car {
-String licensePlate
-String brand
-String model
-String color
-double dailyRate
-boolean available
+isAvailable() boolean
+setAvailable(status) void
+calculateRentalCost(days) double
}
class Customer {
-String customerId
-String name
-String driverLicense
-String phone
-String email
+getContactInfo() String
}
class Rental {
-String rentalId
-Date startDate
-Date endDate
-double totalCost
+calculateDuration() int
+calculateTotalCost() double
+isActive() boolean
}
Customer "1" -- "0..*" Rental : makes
Car "1" -- "0..*" Rental : subject of
E-commerce System
~45 minProblem Description
Design an online shopping system with the following requirements:
- Products - Have a SKU, name, description, price, and stock quantity
- Categories - Products belong to categories (Electronics, Clothing, etc.)
- Customers - Have account info, can have multiple shipping addresses
- Shopping Cart - Customers have a cart with multiple items (product + quantity)
- Orders - Created from cart, have status, shipping address, payment info
- Order Items - Link products to orders with quantity and price at time of order
Your Task
- Identify all necessary classes (there should be at least 6)
- Pay attention to which relationships are composition vs aggregation
- Consider: When an Order is deleted, what happens to its OrderItems?
- Consider: Can a Product exist without a Category?
Key Questions to Answer
- What's the relationship between Cart and CartItem?
- What's the relationship between Order and OrderItem?
- Should Address be its own class or part of Customer?
- How do you represent that OrderItem captures the price at purchase time (not current price)?
Partial Solution (Mermaid)
classDiagram
class Product {
-String sku
-String name
-String description
-double price
-int stockQuantity
+isInStock() boolean
+reduceStock(qty) void
}
class Category {
-String categoryId
-String name
-String description
}
class Customer {
-String customerId
-String email
-String passwordHash
+placeOrder() Order
}
class Address {
-String street
-String city
-String zipCode
-String country
}
Product "*" --> "1" Category : belongs to
Customer "1" *-- "1..*" Address : has
Complete the diagram by adding: Cart, CartItem, Order, OrderItem classes and their relationships.
School Management System
~45 minProblem Description
Design a system for a school with the following entities:
- Persons - Both students and teachers share common attributes (name, birthdate, email)
- Students - Have a student ID, enrollment date, GPA
- Teachers - Have an employee ID, hire date, salary, specialization
- Courses - Have a code, name, credits, max capacity
- Enrollments - Track which students are in which courses, with grade
- Teachers can teach multiple courses; a course has one primary teacher
- Departments - Group teachers and courses; each has a head (a teacher)
Your Task
- Use inheritance appropriately (Person as parent class)
- Identify association classes (hint: Enrollment)
- Model the Department relationships carefully
- Include at least one interface (e.g., Gradeable)
Challenge Questions
- How do you represent that a Department has a "head" who is a Teacher?
- How do you show that Student and Teacher both inherit from Person?
- Is the Enrollment class really necessary, or can you just connect Student to Course?
- What interface might both Student and Course implement?
Starting Point (Mermaid)
Start with this basic structure and expand it:
classDiagram
class Person {
<<abstract>>
-String name
-Date birthdate
-String email
}
class Student {
-String studentId
-Date enrollmentDate
-double gpa
}
class Teacher {
-String employeeId
-Date hireDate
-double salary
-String specialization
}
class Course {
-String code
-String name
-int credits
-int maxCapacity
}
class Department {
-String name
}
class Enrollment {
-String grade
}
class Schedulable {
<<interface>>
+getSchedule() Schedule
}
Person <|-- Student
Person <|-- Teacher
Complete the diagram by adding: relationships between Teacher/Course, Department connections, and the Enrollment association class.
Deliverables
For each exercise, submit:
Submission Checklist
Evaluation Criteria
| Criterion | Points | Description |
|---|---|---|
| Class Identification | 20% | All relevant classes identified from requirements |
| Attributes | 20% | Appropriate attributes with correct types and visibility |
| Methods | 15% | Key behaviors identified with correct signatures |
| Relationships | 25% | Correct relationship types and multiplicities |
| UML Notation | 10% | Proper use of UML standard notation |
| Design Decisions | 10% | Clear justification for design choices |
Bonus Challenges
Resources
- UML Class Diagrams Reference
- Mermaid Class Diagram Syntax Guide
- Mermaid Live Editor - Build and test diagrams online