You are on page 1of 27

UML Class Diagram

SE308 (Software Design and Architecture)

Fall 2022
Class
• A class is the construction plan for a set of similar objects that appear in the
system to be specified.
• Classes can characterize, for example, persons (e.g., students), things (e.g.,
buildings), events (e.g., courses or exams) Examples
 Employee: has a name, employee # and
department; an employee is hired, and fired;
an employee works in one or more projects

Employee
name Name (mandatory)
Attributes
(optional) employee#
department
hire()
fire() Operations
assignproject() (optional)
Visibility
Visibility can be:

+ public - Accessible to all


# protected - in Class or subclass
- private - in class method only (not in Subclass)
~ Package/Default Can be called by any class within its package
Notation
• A class is represented by a rectangle
that can be subdivided into three sections

Attribute
Syntax of attribute

Example : firstName: String


+ name: string [1] = “Anon” {readOnly}
+ address: String [1...n] {unique, ordered}
Multiplicities
• Multiplicity defines how many instances can be associated at given moment
0…1 No instance or One instance A flight seat has only one or no passenger
1 Exactly one instance An Order can has only one Customer
0..* OR * Zero or more instance A Class can have zero or more Students
1…* One or more instance (at least A Flight can have one or more passenger
one)

• Operations
• Operations are characterized by their name, their parameters, and the
Operation type of their return value
• + isRegistered (c: Course) :
Boolean

Relationships in Class Diagram
In UML, object interconnections (logical or physical), are
modeled as relationships.

There are three kinds of relationships in UML:

1. Dependencies

2. Associations

3. generalizations / specialization

4. Aggregation / Composition
1. Dependency Relationships
A dependency indicates a semantic relationship between two or more elements. The
dependency from CourseSchedule to Course exists because Course is used in both the
add and remove operations of CourseSchedule.

Dependency is represented by a dashed arrow starting from the dependent class to its
dependency. Multiplicity normally doesn’t make sense on a Dependency
CourseSchedule
Course
add(c : Course)
remove(c : Course)
Dependency - A change in a class affects the change in it's dependent class.

Shapes
Example- Circle is dependent on Shape. If you change
Shape, it affects Circle too. So, Circle has a dependency calArea(c: circle)
Circle
on Shape.
2. Association
• Associations represent possible relationships between the instance of classes.
• They describe which classes are potential communication partners
• Also called using relationship between two or more objects
• Representing with a thin line connecting two classes
• The edge can be labeled with the name of the association optionally followed by the reading
direction, a small, black triangle

Example : A patient may visit one or many doctors, similarly a doctor meet multiple patients
( if a patient died, the existence of doctor will not vanished and in the same way if doctor dies patient will
remain patient )

Example :
The classes Professor and Student are related via the association givesLectureFor (as label). In the role as a
lecturer, a professor has zero or more students and one student has zero or more professors in the role of
lecturer.
Association Contd.
• A Company has no, one, or multiple Person and that an Person is
associated with exactly one Company.
1 * .. 0
Company Person

• A lecturer may issue no, one, or multiple assignments and that an


assignment is issued by exactly one lecturer.

• A lecturer gives at least one lecture and a lecture is given by at least


one lecturer.
2. Generalization Relationships

A generalization connects a subclass


Person
to its superclass. It denotes an
inheritance of attributes and behavior
from the superclass to the subclass and
indicates a specialization in the subclass
of the more general superclass.

Student
Generalization/Specialization
• Generalization hierarchies may be created by generalizing from
specific things or by specializing from general things.

Parent
More general element
Superclass
Ancestor
Base Class
“is a kind of”

Child
Subclass
Descendant
More specific element
Leaf
Example (Association and Generalization)
• An Order Management System describes a particular aspect of the entire
application.

• First of all, Order and Customer are identified as the two elements of the
system. They have a one-to-many relationship because a customer can have
multiple orders.
• Order class is an abstract class and it has two concrete classes (inheritance
relationship) SpecialOrder and NormalOrder.
• The two inherited classes have all the properties as the Order class. In
addition, they have additional functions like dispatch () and receive ().
Order Management System
Aggregation
• An aggregation is a special form of association that is used to express that
instances of one class are parts of an instance of another class.
• Represented by a hollow diamond at the association end of the class that
stands for the “whole”
• Aggregation is weak relationship
• Also called “is part of“ and in other hand “made of” or “Whole”
• Implies a relationship where the child can exist independently of the parent.
• Example: Lecture (parent) and Student (child). Delete the Lecture and the Students still exist.
• Example: Car (parent) and Engine / Wheel(child)
Engine
Car
Wheels
Composition
• Composition is a specialized form of aggregation in which if the parent object
is destroyed
• It is actually a strong type of aggregation and is also referred to as a "death"
relationship.
• Where the child class cannot exist independent of the parent class
• Compositions are denoted by a filled-diamond
• As an example, a house is composed of one or more rooms. If the house is destroyed,
all the rooms that are part of the house are also destroyed as they cannot exist by
themselves.
• Window and its composite objects File Folder

Scrollbar
1 1
Window Titlebar
1 1
Menu
1 1.
A Big Picture!!
Example - Information system of a university
1. Identifying the classes
A university consists of multiple faculties which are composed of 2. Identifying the attributes
various institutes. Each faculty and each institute has a name. An
address is known for each institute. 3. Identifying the relationships
between classes
Each faculty is led by a dean, who is an employee of the university.

The total number of employees is known. Employees have a social Identified classes
security number, a name, and an e-mail address. There is a
distinction between research and administrative personnel.

Research associates are assigned to at least one institute. The field of


study of each research associate is known. Furthermore, research
associates can be involved in projects for a certain number of hours,
and the name, starting date, and end date of the projects are known.
Some research associates teach courses. They are called lecturers.

Courses have a unique number (ID), a name, and a weekly duration


in hours.
We have defined meaningful data types for our
attributes even though these are not included in the
specification.

We also set the visibility of all attributes to public so


that in this phase, we do not have to think about
which attributes are visible from the outside and
which are not.

The attribute counter of the class Employee is defined as


a class attribute as its values do not belong to an
instance.

This attribute is increased when an instance of the


class Employee is created.
Weekly home task (Week2)

Complete this class diagram with the help of the


description given in the book (UML@Classroom)
Page 76-82
Summary - Association / Aggregation / Composition
• Association is a relationship where all objects have their own lifecycle and
there is no owner.
• Example: Consider this class, Teacher and Student relationship, Multiple students can
associate with single teacher and single student can associate with multiple teachers,
but there is no ownership between the objects and both have their own lifecycle. Both
can be created and deleted independently.
Teacher Students

• example, imagine the relationship between a doctor and a patient. A doctor can be
associated with multiple patients and at the same time, one patient can visit multiple
doctors for treatment and/or consultation. Each of these objects have their own life-
cycle and there is no owner. In other words, the objects that are part of the association
relationship can be created and destroyed independently.
Summary - Association / Aggregation / Composition

Aggregation is a specialized form of Association where all objects have their


own lifecycle, but there is ownership OR the objects may or may or be a part of
the whole

• example of Department and teacher. A single teacher can not belong to multiple
departments, but if we delete the department, the teacher object will not be destroyed

• example, an employee may belong to multiple departments in the organization.


However, if the department is deleted, the employee object wouldn't be destroyed.

Note a whole can contain a part but the reverse is not true.
Summary - Association / Aggregation / Composition
• Composition is again specialized form of Aggregation and we can call this as a
“strong” relationship. It is a strong type of Aggregation. Child object does not
have its lifecycle and if parent object is deleted, all child objects will also be
deleted.

• example of relationship between House and Rooms. House can contain multiple rooms
- there is no independent life of room and any room can not belong to two different
houses. If we delete the house - room will automatically be deleted..

• example relationship between Questions and Options. Single questions can have
multiple options and option can not belong to multiple questions. If we delete the
questions, options will automatically be deleted
• Product catalog contain product specification
• Product catalog used by store
• Items described by exactly one product description
• Items stokes in exactly one store but store has many items
• Items contained in sale, sale has at leats one item but one item is in
exactly one sale
• A sale is initiated by exactly one customer and a customers
• Sale has payment information (sale has exactly on payment detail and
vise versa)
Home Heating System
• Room has 1 Thermostat
• Each Thermostat is associated with
0 or more Heaters
• A Heather has exactly one
Thermostat
• ElectricHeater is a specialized Heater
• AubeTH101D is a specialized
Thermostat
University System
• In a university there are different classrooms, offices and
departments. A department has a name and it contains many offices.
• A person working at the university has a unique ID and can be a
professor or an employee. A professor can be a full, associate or
assistant professor and he/she is enrolled in one department.
• Offices and classrooms have a number ID, and a classroom has a
number of seats.
• Every employee works in an office.
Movie- Shop

• Design a system for a movie-shop, in order to handle ordering of movies


and browsing of the catalogue of the store, and user subscriptions with
rechargeable cards.
• Only subscribers are allowed hiring movies with their own card
• Credit is updated on the card during rent operations.
• Both users and subscribers can buy a movie and their data are saved in the
related order.
• When a movie is not available it is ordered .

You might also like