Class Diagram
Begins as a conceptual or analysis class model and evolves to a
design class model
Used throughout the development process. More detail added as
time goes by.
A static view of classes – shows structure: data, operations, and
associations.
Spring 2010
ACS-3913
Ron McFadyen
1
Classes
Represented by a rectangle with possibly 3 compartments
Customer
Customer
getName()
checkCreditRating()
Customer
Customer
Name
Address
Name
Address
getName()
checkCreditRating()
Spring 2010
ACS-3913
Ron McFadyen
2
Classes
Some classes are stereotyped:
«Singleton»
dbFacade
Later in the course we study the Singleton design
pattern. Stereotyping dbFacade as singleton conveys
substantial information about the class and its behaviour.
Some methodologies have 3 stereotypes for “analysis”
classes: boundary, control, entity
Spring 2010
ACS-3913
Ron McFadyen
3
Classes
Abstract Classes
An abstract class is one that is never instantiated. To
indicate an abstract class, the class name is given in
italics or by using an “abstract” stereotype.
Composite
See last 2 examples
Spring 2010
ACS-3913
Ron McFadyen
4
Attributes
an object contains data which are defined as part of the Class
definition
examples:
• Students have names, addresses, etc;
• Courses have titles, descriptions, prerequisite information.
Student
name
address
Rectangle
corner: Point
Level of detail present will depend on whether you are in
analysis or design, and your purposes at the time
Spring 2010
ACS-3913
Ron McFadyen
5
Attributes
To what degree is an attribute visible to other classes?
Private –
Public +
Protected #
Package ~
Student
-name
-address
Spring 2010
ACS-3913
Ron McFadyen
6
Attributes
Default values =
Derived values /
Multiplicity [ ]
Ordering
{ordered}
Uniqueness {unique}
Invoice
-date:Date = today
-/total: Currency
-payments[0..*]: Currency
Spring 2010
ACS-3913
Student
-name
-address[1..3] {unique}
Ron McFadyen
7
Operations.
What are the responsibilities of a class? What can it do?
Visibility
Parameters
Signature
the name, parameters, and return type of the operation
Student
+getName()
+getGPA(term :Term, gpaType: String)
Spring 2010
ACS-3913
Ron McFadyen
8
Associations
• correspond to verbs expressing a relationship between classes
• example
a Library Member borrows a Copy of a Book
•Multiplicities
• we indicate via multiplicities the range of allowable
cardinalities for participation in an association
• examples: 1, 1..*, 0..*, *, 1..3
Spring 2010
ACS-3913
Ron McFadyen
9
Associations
• Names and roles
• you can name the relationship and indicate how to read it
• you can give role names for participating objects
The name of the relationship and the
direction for reading the name
Person
1..*
employee
employer
The role of a Person in this relationship
Spring 2010
1
Works for
ACS-3913
Company
The role of a Company in this relationship
Ron McFadyen
10
Associations
•example:
a Library Member borrows a Copy of a Book
Member
Spring 2010
*
*
borrows
borrower
ACS-3913
Ron McFadyen
Book
11
Associations
• example:
An employee is supervised by an employee
reports to
supervised
*
Employee
0,1 supervisor
A reflexive
association
Spring 2010
ACS-3913
Ron McFadyen
12
Objects
Class instances
An individual object (instance of a class) is shown with
naming information underlined (usually)
A customer named Joe
Joe: Customer
An unnamed customer
: Customer
Spring 2010
ACS-3913
Ron McFadyen
13
Objects
Object state
Consider an instance of a class
Collectively, the specific attribute values and
connections to other objects is known as the state of the
object
Analysis uncovers states of interest. These can be
modeled using statechart diagrams … later
Spring 2010
ACS-3913
Ron McFadyen
14
Generalization
a generalization is a relationship between a general thing (the
superclass or parent class) and a more specific kind of thing
(the subclass or child class)
example:
a StaffMember is a specialized kind of LibraryMember
a StudentMember is a specialized kind of LibraryMember
LibraryMember
StaffMember
Spring 2010
ACS-3913
StudentMember
Ron McFadyen
15
Motivation for partitioning a class into subclasses:
•subclass has additional attributes of interest
•subclass has additional associations of interest
•subclass is operated on, handled, reacted to, or manipulated
differently than the superclass or other subclasses
Spring 2010
ACS-3913
Ron McFadyen
16
Generalization
Multiple subclasses can be grouped to indicate they are related
subclasses
LibraryMember
StaffMember
StudentMember
It then becomes meaningful to consider certain constraints:
complete, incomplete, disjoint, overlapping
Spring 2010
ACS-3913
Ron McFadyen
17
Generalization
LibraryMember
StaffMember
StudentMember
Inheritance of attributes and behaviour:
•everything a LibraryMember can do, a StaffMember can do
•everything a LibraryMember can do, a StudentMember can do
•If a LibraryMember can borrow a book, so can a
StaffMember and a StudentMember
•a StaffMember and a StaffMember have all the attributes the
LibraryMember has, and possibly more
Specialization: there are some things that a specialized class can
do that a LibraryMember cannot
Spring 2010
ACS-3913
Ron McFadyen
18
Every payment, regardless of whether it is cash, credit, or
cheque, has an Amount and it is associated with a Sale
Pays-for
Payment
1
1
Sale
Amount: money
Cash Payment
Credit Payment
*
1
1
1
CreditCard
Spring 2010
Cheque Payment
ACS-3913
Ron McFadyen
Cheque
19
Payment
Amount: money
Cash Payment
Credit Payment
Cheque Payment
The name Payment is italicized - meaning it is an abstract class
An abstract class is a class that will never be instantiated;
only its subclasses can exist
If “Payment” was not in italics then a Payment could exist that is
not a Cash, Credit, or Check payment
Spring 2010
ACS-3913
Ron McFadyen
20
What is the difference:
Payment
Unauthorized
Payment
Payment
Authorized
Payment
Is-in
1
*
PaymentState
Unauthorized State
Spring 2010
ACS-3913
Ron McFadyen
Authorized State
21
Aggregation and Composition
both are associations used to denote that an object from one
class is part of an object of another class
An example of Aggregation: a course is part of an honours
programme. The same module could be part of several
honours courses
*
*
HonoursProgramme
Course
Suppose the course “UML and Patterns” is part of both the
“Software Engineering” and the “Computer Science” honours
programmes
Spring 2010
ACS-3913
Ron McFadyen
22
Aggregation and Composition
Composition is similar to, but stronger than aggregation. If
you specify composition, then you are saying that one object
owns its parts.
*
Board
Square
A Board is made up of several Squares. A Square will belong to just
one Board. If a Board is deleted, then its Squares are deleted too.
What is the multiplicity at the composition end of the association?
Spring 2010
ACS-3913
Ron McFadyen
23
Aggregation and Composition
Consider Invoices and their Invoice Lines
Question: Is the association aggregation or composition?
?
*
Invoice
Spring 2010
?
InvoiceLine
ACS-3913
Ron McFadyen
24
Aggregation and Composition
Consider Sales and their SalesLineItems
Is the association an aggregation or a composition?
?
*
Sale
Spring 2010
?
SalesLineItem
ACS-3913
Ron McFadyen
25
Aggregation and Composition
Consider the General Calendar and its Course Specifications.
Is the association an aggregation or a composition?
Spring 2010
ACS-3913
Ron McFadyen
26
Composite Pattern
A composite is a group of objects in which some objects contain
others; one object may represent groups, and another may
represent an individual item, a leaf.
We will examine the composite pattern later in the course. At
this time, we are many concerned with its structural aspect.
Consider the class diagram that follows. What objects does it
allow us to instantiate and how will they relate to one
another?
Spring 2010
ACS-3913
Ron McFadyen
27
Composite Pattern
Consider a UML class diagram for machines. Machines may be
complex and contain other machines.
PlantFloor 1
*
MachComponent
*
getMachineCount()
1
Machine
MachComposite
components: List
getMachineCount()
getMachineCount()
Spring 2010
ACS-3913
Ron McFadyen
28
Composite Pattern
An object diagram illustrating the machine on floor 5 of the plant
floor5 :Plantfloor
L1: Machine
M1: MachComposite
L2: Machine
M9: MachComposite
L7: Machine
Spring 2010
ACS-3913
Ron McFadyen
L4: Machine
29
Decorator Pattern
The decorator pattern allows us to enclose an object inside another
object. The enclosing object is called a decorator. The other object
is the component, it is the decorated object.
The decorator conforms to the interface of the enclosed component
and so its presence is transparent to the components clients. The
decorator forwards requests to the component, but may perform
some processing before/after doing so.
We will examine the decorator pattern later in the course. At this
time, we are many concerned with its structural aspect.
Consider the class diagram that follows. What objects does it allow
us to instantiate and how will they relate to one another?
Spring 2010
ACS-3913
Ron McFadyen
30
Decorator Pattern
UML class diagram
1
sale
1
DecoratedReceipt
1
print()
What would a typical
object diagram look
like?
receipt
Decorator
print()
other()
print()
How does the Decorator
pattern differ from the
Composite pattern?
timeOfDay
Spring 2010
ACS-3913
productCoupon
Ron McFadyen
moneySaved
31
Class Diagram
Navigability
This constraint specifies if a direction can be followed – whether
one can navigate from one type of object to another type of object
by following an association path.
*
Student
X
History
-history[0..*] : History
Bidirectional is default
If an association can be navigated then support for that is required
In above we say we can navigate from Student to History, but not
the other way
Spring 2010
ACS-3913
Ron McFadyen
32
Class Diagram
Navigability
In the example below, student could be implemented with a
history attribute:
*
Student
X
History
-history[0..*] : History
history[0..*] provides for the association to be navigated –
a student will know its history, a student can send a
message to a History object
Spring 2010
ACS-3913
Ron McFadyen
33
Class Diagram
Association classes
Used when there is data or behaviour related to the association
Useful for many to many associations
Student
*
*
Section
A student registers for a section and a section has many
students registered
Spring 2010
ACS-3913
Ron McFadyen
34
Class Diagram
Association classes
Suppose we need to indicate a mark is kept to describe the
association
Student
*
*
Section
Class
termMark
examMark
grade
gpa()
Spring 2010
ACS-3913
Ron McFadyen
35
Class Diagram
Association classes
An association class is an association and it is a class– it can
participate in associations
Student
*
*
Section
Class
Exam
Spring 2010
0,1
*
termMark
examMark
grade
gpa()
ACS-3913
Ron McFadyen
36
Class Diagram
Association classes
For a given student and section there can be only one occurrence
of Class. This rule is quite strict or severe, but could be what we
require here for our business rules.
Student
*
*
Section
Class
Exam
Spring 2010
0,1
*
termMark
examMark
grade
gpa()
ACS-3913
Ron McFadyen
37
Class Diagram
Association classes
Consider the following where the *-* is for Student and Course
Student
*
*
Class
Exam
0,1
*
term
section
grade
Course
With only one occurrence
of class for a given student
and course, we would only
be keeping the most recent
values for term, grade, etc.
gpa()
Spring 2010
ACS-3913
Ron McFadyen
38
Class Diagram
Association classes
To allow more flexibility, the modeler might promote his/her
association class to a full class, as in:
Student
1
1
Course
*
*
Class
Exam
0,1
*
term
section
grade
gpa()
Spring 2010
ACS-3913
Ron McFadyen
39
Class Diagram
N-ary associations
An n-ary association is an association among 3 or more classes
Section
*
Student
*
*
Term
enrollment
Spring 2010
ACS-3913
Ron McFadyen
40
Class Diagram
N-ary associations
Each instance of the association is an n-tuple of values from the
respective classes. For each association we have one student, one
section, one term
The multiplicity on a role represents the potential number of
instance tuples in the association when the other n-1 values are
fixed.
For a given student and term, we
have many sections, …
Section
*
Student
*
*
Term
enrollment
Spring 2010
ACS-3913
Ron McFadyen
41
Class Diagram
N-ary associations
Consider a team, goalies, and the season. We could record the
performance of each goalie for each team and each season.
Year
season *
Team
*
*
goalie
team
Player
Record
goalsFor
goalsAgainst
Wins
Losses
ties
Spring 2010
ACS-3913
Ron McFadyen
42
Class Diagram
Comparing different models
We’ll examine the ternary association from last day, and two other
models.
Often ternary examples appear in texts without any real context.
We will think now of the goalie statistics in a larger framework.
To determine which model is best or better means we have to
consider the business rules that are reflected in a model. What
business rules do we need in a hypothetical system?
Spring 2010
ACS-3913
Ron McFadyen
43
Class Diagram
Model A
N-ary associations
Consider a team, its goalies, and the season. We could record the
performance of each goalie for each team and each season.
Year
season *
Team
*
*
goalie
team
Record
goalsFor
goalsAgainst
Wins
Losses
ties
Spring 2010
ACS-3913
Ron McFadyen
Player
A ternary association
capturing an interaction
amongst 3 objects.
This model may only be
good for capturing goalie
statistics by team, by season
44
Class Diagram
Model B
Now, consider binary associations only
Consider some other business rules:
BR: Each season teams register to participate in the sports league.
Year
*
season
*
team
Team
Note that we could use an association class for additional
attributes to describe the association, or for the association
participate in associations
Note that this business rule wasn’t covered in the Model A
Spring 2010
ACS-3913
Ron McFadyen
45
Class Diagram
Model B
Using binary associations
Consider business rules:
BR: Each year each registered team has players and some of these
will play as goalies. Note that a goalie could play on multiple
teams each year.
Year
* registers
season
RegTeam
*
team
Team
* playsAsGoalie
*
team
goalie
Player
Now we are also capturing the fact that registered teams have
goalies
Spring 2010
ACS-3913
Ron McFadyen
46
Class Diagram
Model B
Using binary associations
Consider business rules:
BR: Each registered team keeps statistics for each goalie
Year
*
season
*
team
RegTeam
Team
*
team
*
goalie
Player
Record
goalsFor
goalsAgainst
Wins
Losses
ties
Spring 2010
ACS-3913
Ron McFadyen
47
Class Diagram
Model B
Using binary associations
It’s clear with this model that we are recording registered teams
separately from the performance statistics of goalies.
Model A does not facilitate team-related information. RegTeam is
a place to keep information such as president, colours, homeField
Year
*
season
*
team
RegTeam
Team
*
team
*
goalie
Player
team
Record
goalsFor
goalsAgainst
Wins
Losses
ties
Spring 2010
ACS-3913
Ron McFadyen
48
Class Diagram
Model C, another alternative
One binary association between Year and Team, and a 3-ary
(ternary) association amongst Year, Team, and Player
RegTeam
Year
*
season
*
team
*
season
Team
team
*
*
goalie
Record
goalsFor
goalsAgainst
Wins
Losses
ties
Spring 2010
ACS-3913
Player
Which model is better, model
B or model C?
Does model C allow some
connections that model B does
not?
Ron McFadyen
49
Object Diagram
An object diagram is an object graph showing objects (possibly
named and including attribute values) and links.
A link shows a connection from one object to another.
e.g. a class diagram
Consider a UML class diagram for machines. Machines may be
complex and contain other machines.
PlantFloor 1
*
MachComponent
*
getMachineCount()
1
Machine
MachComposite
components: List
getMachineCount()
getMachineCount()
Spring 2010
ACS-3913
Ron McFadyen
51
e.g. One object diagram
An object diagram illustrating the machine on floor 5 of the plant
floor5 :Plantfloor
M1: MachComposite
links
objects
L1: Machine
L2: Machine
M9: MachComposite
L7: Machine
Spring 2010
ACS-3913
Ron McFadyen
L4: Machine
52
Links, objects, object diagrams
Section 7.8.1
Goes through a sequence of diagrams illustrating how an
analysts works out the solution to a modeling problem
Analyst constructs object diagrams, then decides on the
proper class diagram.
Figures 7-27, 28, 29, 30
7-27 first draft – has a problem with the address objects
7-28 second draft – happens to use containment notation for
composition – analyst clarifies role of addresses in the solution
7-29 above revised, but now correctly drawn
7.30 class diagram that fits the final object diagram
Spring 2010
ACS-3913
Ron McFadyen
53
Links, objects, object diagrams
Section 7.8.1
Figure 7-30
CarSharer
startsAt
1
1
registers
*
1
Journey
Address
1
1
endsAt
-homeAddress
1
Spring 2010
ACS-3913
Ron McFadyen
54
Interfaces
Section 7.5.5
An interface is special type of class that cannot be instantiated.
An application can never instantiate an interface.
An interface defines a set of public attributes and operations that
some class must use (depends on)
There is no behaviour defined, no method coded
Some other class inherits the interface and provides the
implementation
Spring 2010
ACS-3913
Ron McFadyen
55
From Head First …
package headfirst.observer.weatherobservable;
import java.util.Observable;
import java.util.Observer;
public class ForecastDisplay implements Observer,
…
public void update(Observable observable, Object arg) {
if (observable instanceof WeatherData) {
WeatherData weatherData = (WeatherData)observable;
lastPressure = currentPressure;
currentPressure = weatherData.getPressure();
display();
}
}
public void display() {
System.out.print("Forecast: ");
if (currentPressure > lastPressure) {
System.out.println("Improving weather on the way!");
}…
Spring 2010
ACS-3913
Ron McFadyen
56
From Head First …
package headfirst.observer.weatherobservable;
import java.util.Observable;
import java.util.Observer;
public class StatisticsDisplay implements Observer, DisplayElement {
private float maxTemp = 0.0f;
…
public void update(Observable observable, Object arg) {
if (observable instanceof WeatherData) {
WeatherData weatherData = (WeatherData)observable;
float temp = weatherData.getTemperature();
…
display();
}
}
public void display() {
System.out.println("Avg/Max/Min temperature = " …
Spring 2010
ACS-3913
Ron McFadyen
57
From Head First …
<<interface>>
Observer
update()
These classes implement the
update operation
StatisticsDisplay
Spring 2010
ForecastDisplay
ACS-3913
Ron McFadyen
58
Sequence Diagram
•Objects are represented horizontally across the top of the
diagram
•Each object has a lifeline
•some exist before and/or after
•some are created during
•some are destroyed during
•An active object is indicated by a narrow rectangle
•Focus of control
•Time is represented vertically down the diagram. Time
moves forward as you go downwards
•Iteration is shown with a frame
Spring 2010
ACS-3913
Ron McFadyen
59
Sequence Diagram
Reading:
See sections 9.1, 9.2, 9.3, 9.4.1-9.4.4,
9.5 pages 178-182
Message types
•Synchronous
•Asynchronous
•Creation
•Reply
Spring 2010
ACS-3913
Ron McFadyen
60
Diagramming notation
:sale
:sale
s4:sale
An unnamed
sale object
s4
s4
s4:sale
A sale object
named s4
Sale
An object
named s4
Spring 2010
The class Sale
ACS-3913
Ron McFadyen
61
Sequence Diagram named Make payment
sd Make payment
: register
: sale
makePayment()
makePayment()
payment()
: payment
Objects that pre-exist the collaboration are shown at top of diagram.
Other objects are shown where/when they are created
Spring 2010
ACS-3913
Ron McFadyen
62
: sale
getTotal()
calcTotal()
Message to ‘self’
- reflexive message
Spring 2010
ACS-3913
Ron McFadyen
63
:SalesLineItem
:Sale
t=getTotal()
loop [I < numItems]
st = getSubTotal()
Iteration
loop is one of the keywords you can use to specify the nature
of the fragment
Note page 167 and use of alt
Spring 2010
ACS-3913
Ron McFadyen
64
e.g. Singleton pattern
Suppose CarMatchOffice works according to the Singleton
Pattern.
This means there must be at any time, at most, one
instance of CarMatchOffice.
The getInstance operation returns the single instance of the
class.
getInstance will create the instance if it does not already
exist.
<<Singleton>>
CarMatchOffice
instance
getInstance()
Spring 2010
ACS-3913
Ron McFadyen
65
client
alt
CarMatchOffice
When you ask for the
instance of
CarMatchOffice, there are
two ways it can complete.
[inst == null]
getInstance()
CarMatchOffice()
:CarMatchOffice
Inst=CarMatchOffice()
[else]
getInstance()
getAddress()
Figure 17-21. Behaviour in Singleton Pattern
Spring 2010
ACS-3913
Ron McFadyen
66
•
Examples 9.1-9.8 pages 179-182
•
Discuss construction of a sequence diagram
•
Problem: create a sequence diagram to model the messages
that occur when a Journey is created
Figure 9-43 shows a diagram with CarSharer, Address,
Journey, …
Example 9-8 shows a scenario where the addresses are
geolocated. Note that geolocate() is a method that manages this.
•
•
Spring 2010
ACS-3913
Ron McFadyen
67
© Copyright 2026 Paperzz