Lecture 02: Class diagram
Doan Trung Tung, PhD
OO
AD OBJECT-ORIENTED ANALYSIS AND DESIGN 1
OO
AD
PLAN
Class diagram
Relationships
OBJECT-ORIENTED ANALYSIS AND DESIGN
From requirements to Class diagram
2
OO
AD
What is a Class Diagram?
A class diagram describes the
types of objects in the system and
OBJECT-ORIENTED ANALYSIS AND DESIGN
the various kinds of relationships
that exist among them.
A graphical representation of a
static view on static elements.
Richest notation in UML
3
OO
AD
Objects
Objects have three responsibilities:
OBJECT-ORIENTED ANALYSIS AND DESIGN
o What they know about themselves – (e.g., Attributes)
o What they do – (e.g., Operations)
o What they know about other objects – (e.g., Relationships)
4
OO
AD
Class
A class is a template (specification, blueprint) for a collection of
objects that share a common set of attributes and operations.
OBJECT-ORIENTED ANALYSIS AND DESIGN
MovieClubMember
Class attributes
operations
Objects
5
OO
AD
Class notation in class diagram
Class name
OBJECT-ORIENTED ANALYSIS AND DESIGN
private
Attributes
protected
public Methods
6
OO
AD
Generalization relationship
A generalization connects a subclass to its superclass. It
denotes an inheritance of attributes and behaviors from
OBJECT-ORIENTED ANALYSIS AND DESIGN
the superclass to the subclass and indicates a
specialization in the subclass of the more general
superclass.
Can be viewed as “is a” relationship
Student Person
7
OO
AD
Generalization relationship
UML permits a class to inherit from multiple superclasses,
although some programming languages (e.g., Java) do not
OBJECT-ORIENTED ANALYSIS AND DESIGN
permit multiple inheritance.
Student Employee
TeachingAssistant
8
OO
AD
Generalization relationship
Abstract class
OBJECT-ORIENTED ANALYSIS AND DESIGN
Shape
draw()
Circle Rectangle
draw() draw()
9
OO
AD
Generalization relationship
Discriminator
Discriminator
OBJECT-ORIENTED ANALYSIS AND DESIGN
Doctor
Female
role
Person Nurse
Gender
Male {complete}
patient
Physical-
therapist
Patient
10
OO
AD
Associations relationship
Relationships between instances (objects)
Association describes a link, a link being a connection
OBJECT-ORIENTED ANALYSIS AND DESIGN
among objects between classes.
Can be viewed as “has a” relationship
11
OO
AD
Associations relationship
Properties
o Name
OBJECT-ORIENTED ANALYSIS AND DESIGN
o Role
› The specific role of the association
o Multiplicity
› Indicates the number of objects that are connected
o Type
› Plain association, aggregation, composition
o Direction
12
OO
AD
Associations
Example: A Person works for a Company
OBJECT-ORIENTED ANALYSIS AND DESIGN
Role
employee employer
Person Company
works for
Association Name
13
OO
AD
Associations
Multiplicity: specify properties of the number of links that
can exist between instances (objects) of the associated
OBJECT-ORIENTED ANALYSIS AND DESIGN
classes.
Association name
Class1 Class2
multiplicity multiplicity
Multiplicity notation
o* 0, 1, or more
o5 5 exactly
o 5..8 between 5 and 8, inclusive
o 5..* 5 or more
14
OO
AD
Associations
Multiplicity example
o A Student can take up to 5 Courses.
OBJECT-ORIENTED ANALYSIS AND DESIGN
o Student has to be enrolled in at least one course.
o Up to 300 students can enroll in a course.
o A class should have at least 10 students.
Student Course
10..300 takes 1..5
15
OO
AD
Associations
Multiplicity example
o A teacher teaches 1 to 3 courses (subjects)
OBJECT-ORIENTED ANALYSIS AND DESIGN
o Each course is taught by only one teacher.
o A student can take between 1 to 5 courses.
o A course can have 10 to 300 students.
1 teaches 1..3
Teacher Course
1..5
takes
Students
10..300
16
OO
AD
Self association
An association that connects a class to itself is called a
self association.
OBJECT-ORIENTED ANALYSIS AND DESIGN
1
Employee
manager
Responsible for worker
0..10
17
OO
AD
Multi associations
A cricket team has 11 players. One of them is the captain.
A player can play only for one Team.
OBJECT-ORIENTED ANALYSIS AND DESIGN
The captain leads the team members.
Captain
Team
Member 1 0..1
Player Team
10
11 member of 1
Captain
1
Leads
18
OO
AD
Aggregation
A specialized form of Association in which a whole is
related to its part(s)
OBJECT-ORIENTED ANALYSIS AND DESIGN
Can be viewed as a “part of” relationship
o Whole – parts
o Container – contents
Can they be association?
o Group - members
Engine
Car
Transmission
19
OO
AD
Composition
Can they be aggregation?
Special Aggregation
Strong ownership
OBJECT-ORIENTED ANALYSIS AND DESIGN
Scrollbar
Window Titlebar
Menu
20
OO
AD
How to identify aggregation / composition?
Lifetime of part is bound within lifetime of composite
o There is a create-delete dependency
OBJECT-ORIENTED ANALYSIS AND DESIGN
There is an obvious whole-part physical or logical
assembly
Some properties of composite propagate to parts (e.g.,
location)
Operations applied to composite propagate to parts (e.g.,
destruction, movement, recording)
21
OO
AD
Why aggregation / composition?
Assists in identification of a creator
Operations applied to whole should usually propagate to
OBJECT-ORIENTED ANALYSIS AND DESIGN
parts
Identifying whole - parts supports encapsulation
22
OO
AD
Dependency
Change in specification of one class can change the other
class. This can happen when one class is using another
OBJECT-ORIENTED ANALYSIS AND DESIGN
class.
Always directed
Caused by class methods
o local variable, parameter, return value
Circle
Point
Move(p:Point)
23
OO
AD
Dependency
Dependency relationship can be used to show
relationships between classes and objects
OBJECT-ORIENTED ANALYSIS AND DESIGN
circleA:Circle
Circle
circleB:Circle
24
OO
AD
Realization (implementation)
A realization relationship indicates that one class
implements a behavior specified by another class (an
OBJECT-ORIENTED ANALYSIS AND DESIGN
interface or protocol).
An interface can be realized by many classes.
A class may realize many interfaces.
25
OO
AD
Realization
Example
OBJECT-ORIENTED ANALYSIS AND DESIGN
Keyboard
brandName <<interface>>
numOfKeys TypeWriter
ctl()
pageDown() keyStroke()
26
OO
AD
Constraint rules and Notes
Constraints and notes annotate among other things
associations, attributes, operations and classes.
OBJECT-ORIENTED ANALYSIS AND DESIGN
Constraints are semantic restrictions noted as Boolean
expressions.
Customer 1 * may be
Order
{ total < $50 } canceled
id: long { value > 0 }
Constraint Note
27
OO
AD
Common mistakes / confusions
When drawing Class Diagram
o Draw classes immediately
OBJECT-ORIENTED ANALYSIS AND DESIGN
o Hard to differentiate relationships
› Associations or
› Aggregation or
› Composition?
o Attributes or classes?
o Inheritances or Interfaces?
o Draw all dependencies?
o…
28
OO
AD
From user requirements to Class Diagram
“A class diagram describes the types of objects in the system
and the various kinds of static relationships that exist among
OBJECT-ORIENTED ANALYSIS AND DESIGN
them”
Make a list of objects found in user requirements
Group similar objects into class
Define relationships between each two classes
o Multiplicity
o Type (generalization, association, realization, dependency)
o Name, direction, …
29
OO
AD
User requirements …
Zounds international (Audiovisual sales) Has A number of
(sub) divisions such as Purchasing, NetSales, Stock, Retail
OBJECT-ORIENTED ANALYSIS AND DESIGN
etc. ‘Retail’ has outlets (e.g. in Malls and High street stores).
NetSales has both ‘Hard’ (CDs, DVDs…) and ‘soft’
(downloads) sales. Retail and Netsales co-operate to
produce ‘Personal mixes’ that they burn onto CDs to
produce items for individual customers. Customers may
open an account to facilitate the payment for multiple
transactions or they may just buy goods in individual
purchases paying by cash, cheque or card. The staff selling
the good are recorded.
30
OO
AD
User requirements …
Zounds international (Audiovisual sales) Has A number of
(sub) divisions such as Purchasing, NetSales, Stock, Retail
OBJECT-ORIENTED ANALYSIS AND DESIGN
etc. ‘Retail’ has outlets (e.g. in Malls and High street stores).
NetSales has both ‘Hard’ (CDs, DVDs…) and ‘soft’
(downloads) sales. Retail and Netsales co-operate to
produce ‘Personal mixes’ that they burn onto CDs to
produce items for individual customers. Customers may
open an account to facilitate the payment for multiple
transactions or they may just buy goods in individual
purchases paying by cash, cheque or card. The staff selling
the good are recorded.
31
OO
AD
User requirements …
A sub-division may also be a parent division e.g. Zounds UK
is a sub-division of Zounds international but also a parent
OBJECT-ORIENTED ANALYSIS AND DESIGN
of Zounds Sweet (specialising in musical confectionary). ZI
has no parent division. Retail staff display and sell traditional
stock items (CD, DVD) in the retail outlets to customers
(these people may also be staff). The netSales staff maintain
and sell copies of ‘softstock’ (digitally encoded mediaware
that they are licensed to distribute) direct to customers (or
staff). The company’s retail outlets purchase ‘SoftStock’ for
the mixes.
32
OO
AD
User requirements …
A sub-division may also be a parent division e.g. Zounds UK
is a sub-division of Zounds international but also a parent
OBJECT-ORIENTED ANALYSIS AND DESIGN
of Zounds Sweet (specialising in musical confectionary). ZI
has no parent division. Retail staff display and sell traditional
stock items (CD, DVD) in the retail outlets to customers
(these people may also be staff). The netSales staff maintain
and sell copies of ‘softstock’ (digitally encoded mediaware
that they are licensed to distribute) direct to customers (or
staff). The company’s retail outlets purchase ‘SoftStock’ for
the mixes.
33
OO
AD
Objects
• Divisions • Hard sales • Customers
• Subdivisions • Soft sales • Staff
OBJECT-ORIENTED ANALYSIS AND DESIGN
• Retails • CDs • Retail staff
• Netsales • Mixes • Netsale staff
• Purchasings • Items
• Stocks • Goods • Accounts
• DVD
• Transactions
• Purchases • Cheque
• Cash
• Card
34
OBJECT-ORIENTED ANALYSIS AND DESIGN
AD
OO
Classes
35
OO
AD
User requirements …
The divisions and subdivisions each have a single manager and may
have staff. Staff are employed by Zounds international and are
OBJECT-ORIENTED ANALYSIS AND DESIGN
assigned to exactly one division. There are many types of staff and
their assignment/role (Cleaner, NetSales, Manager) may vary during
their time with ZI. All subdivisions have only one parent division but
parent divisions may have many subdivisions. Theoretically all the
parent divisions have control over all their subdivisions, though in
practice this is seldom enforced for the mundane ‘day-to-day’
running of the company. All Sub-divisions are cost centres,
maintaining their own accounts but constrained to follow a
centralised accounting model with a standard way of maintaining
the balance and transaction history and recording transactions.
36
OO
AD
User requirements …
The divisions and subdivisions each have a single manager and may
have staff. Staff are employed by Zounds international and are
OBJECT-ORIENTED ANALYSIS AND DESIGN
assigned to exactly one division. There are many types of staff and
their assignment/role (Cleaner, NetSales, Manager) may vary during
their time with ZI. All subdivisions have only one parent division but
parent divisions may have many subdivisions. Theoretically all the
parent divisions have control over all their subdivisions, though in
practice this is seldom enforced for the mundane ‘day-to-day’
running of the company. All Sub-divisions are cost centres,
maintaining their own accounts but constrained to follow a
centralised accounting model with a standard way of maintaining
the balance and transaction history and recording transactions.
37
OO
AD
OBJECT-ORIENTED ANALYSIS AND DESIGN Classes and relationships
Do you agree with type of associations?
38
OO
AD
Hidden objects/classes
Web pages
Windows Remember class UITool?
OBJECT-ORIENTED ANALYSIS AND DESIGN
Dialogs
“Data Access” Classes
…
39
OO
AD
Exercise
The ABC bookstore offers a Web system that allows its customers to
search, reserve, buy and rent books online. Any customer can visit the
bookstore’s official Web site to search for book details, but only the
OBJECT-ORIENTED ANALYSIS AND DESIGN
registered customers (hereinafter, members) can reserve, buy and rent
books online. The required information for a membership is the
customer’s detail information such as name, address, a unique social ID,
phone number, e-mail address, and credit card number. Once the
registration is completed, the system will send the confirmation notification
to the customer’s e-mail address. The ABC bookstore offers services for
both hard-copied books and electronic books. The rental service is
applied for hard-copied books only. For hard-copied books, a customer
can search for book information and reserve the books that he/she wants
to buy or rent. The reserved time is 1 day. After the customer made the
reservation, he/she must go to the bookstore to pick up the books within
the reserved time. Otherwise, the reserved books will be released to
other customers. To pick up the books, the customer must present the
reservation number to the bookstore’s staff and pay for the books. 40
OO
AD
Exercise
The rental period is 7 days for each book. To return the books,
the customer can either go to the bookstore during its office
hours (8:00 to 22:00) to return them to the staff at the counter
OBJECT-ORIENTED ANALYSIS AND DESIGN
service, or use an automated self-service book return box that is
located in front of the bookstore and is available for 24 hours.
The book return box has a barcode reader attached to it. The
customer has to use the bar code reader to scan a barcode of
each book and then put the book into the book return box. If the
customer returns the books later than the return date, he/she will
be charged at $10 per day per book via his/her credit card. The
late-returned fee will be shown in his/her membership account.
In case of buying books online (both hard-copied books and
electronic books), the customer must make payment using
his/her credit card and also provide a shipping address for the
books delivery.
Draw class diagram (use CRC first). Make your own assumptions if needed 41