Lec6 - UML Diagrams - Final
Lec6 - UML Diagrams - Final
UML
Modeling with Classes
UML diagrams
Use case diagrams
• Describe user tasks and points of contact with the
system
Class diagrams
• describe classes and their relationships
Sequence diagrams
• show the behaviour of systems as interactions
between objects
State diagrams and activity diagrams
• show how systems behave internally
Component and deployment diagrams
• “big picture” of how the components of a system are
related 2
Usecase diagram
Use Cases
• Two types of Actors: Users and System administrators
System
Use Case
Use Case
Actor (People) Actor (Systems)
Use Case
Use case examples
(use cases for powerpoint.)
About the last example...
• Gives a view of Powerpoint.
• focuses your attention to the key features
HW: Make a detailed Use case for the ATM based on the diagram in the next 2 slides.
Can you find any items which are common to multiple Use cases ( eg.
Withdraw+Deposit= Transfer)
Are there any more Hierarchical Use cases?
(A few of you, NOT ALL can go to the ATM machine and get the details)
Usecase diagram for an ATM machine
Usecase diagram for an ATM machine
Usecase diagram for an Online Purchase
Association with Stereotypes:
Uses, Extends and Includes
• Uses or modifies or updates : (Side 7)
– Direction indicates an entity doing the action on
another entity eg: Designer <<modifies>> a Web
interface
– Open arrow is used
Relations between entities
classA <|-- classB Super Class (A) Sub Class (B)
classC *-- classD Composition
classE o-- classF Aggregation
classG <-- classH Directed Association
classI -- classJ Association
classK <.. classL Directed association between Interfaces
classM <|.. classN M is implemented by class N
classO .. classP Association between Interfaces classes
Class Diagrams
Essentials of UML Class Diagrams
• The main symbols shown on class diagrams are:
– Classes
• represent the types of data themselves
– Attributes
• are simple data found in classes and their
instances
– Operations
• the functions performed by classes and their
instances
– Associations
• represent linkages between instances of classes
– Generalizations
• group classes into inheritance hierarchies 16
Classes
• A class is simply represented as a box with the
name of the class inside
– The diagram may also show the attributes
and operations
– The UML signature of an operation is:
operationName(parameterName: parameterType …): returnType
17
Associations and Multiplicity
• An association is a line that relates two classes
• Symbols indicating multiplicity are shown at
each end
Employee * Company
Company BoardOfDirectors
18
Labelling associations
An association can be labeled, to clarify its nature
* works For
Employee Company
Company BoardOfDirectors
0..1 allocatedTo *
Office Employee
19
Interpreting associations
Many-to-one
• A company has many employees,
• An employee can only work for one company.
– No moonlighting!
• A company can have zero employees
– E.g. a ‘shell’ company
• Every employee must work for some company
* works For
Employee Company
20
Interpreting associations
Many-to-many
• A secretary can work for many managers
• A manager can have many secretaries
• Secretaries can work in pools
• Managers can have a group of secretaries
• Some managers might have zero secretaries.
• Is it possible for a secretary to have, perhaps
temporarily, zero managers?
21
Interpreting associations
One-to-one
• For each company, there is exactly one board of
directors
• A board is the board of only one company
• A company must always have a board
• A board must always be of some company
Company BoardOfDirectors
22
Another example
A booking is always for exactly one passenger
• no booking with zero passengers
• a booking could never involve more than one
passenger.
A Passenger can have any number of Bookings
• a passenger could have no bookings at all
• a passenger could have more than one booking
23
Association classes
Sometimes, an attribute shared by two
associated classes cannot be placed in either
one. E.g., the following are equivalent:
Student * * CourseOffering
Registration
grade
24
Reflexive associations
An association can connect a class to itself
successor *
* Course *
isMutuallyExclusiveWith
*
prerequisite
25
Directionality in associations
Associations are by default bi-directional
The direction can be limited by adding an
arrow:
Day * Note
26
Generalization
• Specializing a superclass into two or more
subclasses
The label that describes the criterion for
specialization
Animal Animal
27
Handling multiple discriminators
Creating higher-level generalization
Animal
habitat
AquaticAnimal LandAnimal
typeOfFood typeOfFood
28
Handling multiple discriminators
Using multiple inheritance
Animal
habitat typeOfFood
29
30
Visibility of Class Members
Association
• An association is a relationship between two separate
classes. It joins two entirely separate entities.
• There are four different types of association:
– Bi-directional
– Uni-directional
– Aggregation (includes composition aggregation)
– Reflexive.
• Bi-directional and uni-directional associations are the
most common ones.
• This can be specified using multiplicity (one to one,
one to many, many to many, etc.).
• Implementation in Java: use of an instance field. The
relationship can be bi-directional with each class
holding a reference to the other.
32
Association
* 40
Student Course
has enrolls
Class Student {
Course enrolls[40];
}
Class Course {
Student has[];
}
33
Composition
Class Chessboard {
Square[64];
}
Class Square {
Color c;
}
34
Aggregation
Vehicle * VehiclePart
Country * Region
35
When to use an aggregation
• An association is an aggregation if:
– You can state that
• the parts ‘are part of’ the aggregate
• or the aggregate ‘is composed of’ the parts
– When something owns or controls the
aggregate, then they also own or control the
parts
36
Composition
• A composition is a strong kind of aggregation
• if the aggregate is destroyed, the parts are also
destroyed. Highly dependent on each other.
Building * Room
37
Aggregation hierarchy
Vehicle
* *
Chassis BodyPanel Door
*
Frame Engine Transmission Wheel
38
Inheritance
• Indicates that child (subclass) is considered to be a
specialized form of the parent (super class).
39
Interfaces
• An interface describes a portion of the visible
behaviour of a class.
• An interface is similar to a class, except it lacks
instance variables and method bodies
«interface»
Person Cashier Machine Person Machine
withdraw
deposit Cashier Cashier
40
Interface
• A relationship between two model elements, in which one
model element implements/executes the behavior that the
other model element specifies
41
• A house has exactly one kitchen, exactly one bath, at
least one bedroom (can have many), exactly one
mailbox, and at most one mortgage (zero or one).
Modelling Interactions and
Behaviour
Interaction Diagrams
• Interaction diagrams are used to model the dynamic
aspects of a software system
– They help you to visualize how the system runs.
– An interaction diagram is often built from a use
case and a class diagram.
• The objective is to show how a set of objects
accomplish the required interactions with an actor.
47
Interactions and messages
– Interaction diagrams show how a set of actors and
objects communicate with each other to perform:
• The steps of a use case, or
• The steps of some other piece of functionality.
– Actors
• Use the stick-person symbol as in use case diagrams
– Messages
• Shown as arrows from actor to object, or from object
to object
49
Sequence diagrams – an example
:CourseSection :Student
requestToRegister :Registration
<<create>>
addToSchedule
addToRegistrationList
50
Sequence diagrams
• A sequence diagram shows the sequence of messages exchanged by the
set of objects performing a certain task
– The objects are arranged horizontally across the diagram.
– An actor that initiates the interaction is often shown on the left.
– The vertical dimension represents time.
– A vertical line, called a lifeline, is attached to each object or actor.
– The lifeline becomes a broad box, called an activation box during the
live activation period.
– A message is represented as an arrow between activation boxes of the
sender and receiver.
• A message is labelled and can have an argument list and a return
value.
51
Sequence diagrams –
same example, more details
GUI :CourseSection aStudent: :Course
Student
requestToRegister
requestToRegister
prereq :=
(aStudent)
hasPrerequisite := getPrerequisite
hasPassedCourse(prereq)
[hasPrerequisite]
<<create>> :Registration
addToRegistrationList addToSchedule
52
Sequence diagrams
– An iteration over objects is indicated by an
asterisk preceding the message name
co mputeTo tal
53
Object deletion
– If an object’s life ends, this is shown with an X at
the end of the lifeline
:Spe cificFlight :Boo king :Passe ngerRole
ca ncelBooking
ca ncel
deleteFromItinerary
deleteFromPassenge rList
54
Statechart diagram
State Diagrams
State Diagrams show the sequences of states an
object goes through during its life cycle in
response to stimuli, together with its responses
and actions; an abstraction of all possible
behaviors.
Start End
Unpaid Paid
Invoice created paying Invoice destroying
Special States
• The initial state is the state entered when an object is
created.
– An initial state is mandatory.
Locked
Events, Actions & Transitions
• An event: stimulus that can trigger a state transition.
Locked Unlocked
coin/unlock
Cancel [ count = 10 ]
Canceled
do: Notify registered Closed
students Cancel
do: Finalize course
State Diagrams
• A state diagram describes the behaviour of a system,
some part of a system, or an individual object.
– At any given point in time, the system or object is
in a certain state.
• Being in a state means that it is will behave in a specific
way in response to any events that occur.
– Some events will cause the system to change
state.
• In the new state, the system will behave in a different
way to events.
– A state diagram is a directed graph where the
nodes are states and the arcs are transitions. 61
State diagrams – an example
e
XWin
XTurn
Tie
OWin
OTurn
62
State diagrams – an example of transitions
with time-outs and conditions
GreenLightNoTrigger
vehicleWaitingToTurn
GreenLight GreenLightChangeTriggered
after(5s) after(5s)
RedLight RedLight
63