0% found this document useful (0 votes)
44 views63 pages

Lec6 - UML Diagrams - Final

The document discusses UML class diagrams and their components like classes, attributes, operations, associations, generalizations, and visibility of class members. It provides examples and explanations of how to interpret different types of associations and generalizations in class diagrams.

Uploaded by

Yashaswi Piplani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views63 pages

Lec6 - UML Diagrams - Final

The document discusses UML class diagrams and their components like classes, attributes, operations, associations, generalizations, and visibility of class members. It provides examples and explanations of how to interpret different types of associations and generalizations in class diagrams.

Uploaded by

Yashaswi Piplani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

Object Oriented Programming:

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 Powerpoint by


clicking on the Menu items.
– Can you find any items which are common to
multiple Use cases?
– Are there any Hierarchical Use cases?
Use case examples
(Relationships in a news web site.)
About the last example...
• The last is more complicated and realistic use case diagram. It
captures several key use cases for the system.
• Note the multiple actors. In particular, ‘AP wire’ is an actor,
with an important interaction with the system, but is not a
person (or even a computer system, necessarily).
• The notes between << >> marks are stereotypes
• HW: Make a detailed Use case for User login and a
second database to store user profiles.
Usecase diagram- ATM machine
• An automated teller machine (ATM) provides bank customers with access to
financial transactions.

• Customer uses bank ATM to


• Check Balances
• Deposit Funds,
• Withdraw Cash
• Transfer Funds

• ATM Technician provides Maintenance and Repairs.

• Bank actor: customer transactions or to the ATM servicing.

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

Rectangle Rectangle Rectangle Rectangle Rectangle


getArea height height height: int
resize width width width : int
getArea getArea(): int
resize resize(int,int)

17
Associations and Multiplicity
• An association is a line that relates two classes
• Symbols indicating multiplicity are shown at
each end
Employee * Company

Secretary * 1..** Manager

Company BoardOfDirectors

Office 0..1 * Employee

Person 0,3..8 * BoardOfDirectors

18
Labelling associations
An association can be labeled, to clarify its nature
* works For
Employee Company

Secretary * 1..** Manager


supervisor

Company BoardOfDirectors

0..1 allocatedTo *
Office Employee

Person 0,3..8 * BoardOfDirectors


boardMember

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?

Secretary * 1..** Manager


supervisor

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

Passenger * Booking * SpecificFlight

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

Student * Registration * CourseOffering


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

habitat typeOfF ood

AquaticAnimal LandAnimal Carnivore Herbivore

27
Handling multiple discriminators
Creating higher-level generalization

Animal

habitat

AquaticAnimal LandAnimal

typeOfFood typeOfFood

AquaticCarnivore AquaticHerbivore LandCarnivore LandHerbivore

28
Handling multiple discriminators
Using multiple inheritance
Animal

habitat typeOfFood

AquaticAnimal LandAnimal Carnivore Herbivore

AquaticCarnivore AquaticHerbivore LandCarnivore LandHerbivore

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

Aggregations are associations that represent ‘part-


whole’ relationships.
• The ‘whole’ side is often called the assembly or the
aggregate
• This symbol is a shorthand notation association named
isPartOf

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

Employee ATM Employee ATM

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.

– The set of steps, taken together, is called an


interaction.

– Interaction diagrams can show several different


types of communication.
• E.g. method calls, messages send over the network
• These are all referred to as messages. 48
Elements found in interaction
diagrams
– Instances of classes
• Shown as boxes with the class and object identifier
underlined

– 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

Course * CourseSection * Registration * Student


getPrerequisite requestToRegister addToSchedule
addToRegistrationList hasPassedCourse

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

:Bill :Pur chase :Item

*[for all Purch ase] g etSubtotal


getUnitPrice

co mputeTo tal

Bill * Purchase * Item


quan tity price

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.

– Only one initial state is permitted.

– The initial state is represented as a solid circle.

• A final state indicates the end of life


– A final state is optional.

– A final state is indicated by a bull’s eye.

– More than one final state may exist.

Locked
Events, Actions & Transitions
• An event: stimulus that can trigger a state transition.

• A transition: is a change from an originating state to a successor


state as a result of some stimulus.
– The successor state could possibly be the originating state.

• A transition may take place in response to an event.


• Transitions can be labeled with event names.

Locked Unlocked
coin/unlock

Transition Event Name/ action


Statechart Diagram
• A statechart diagram shows the lifecycle of a single class.

Add student[ count < 10 ]


Add Student /
Initialization Set count = 0
Open
do: Initialize course entry: Register student
exit: Increment count
Cancel

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(25s since exit from


after(25s) state RedLight)

YellowLight after(30s) YellowLight after(30s)

after(5s) after(5s)

RedLight RedLight

63

You might also like