Introduction To UML
Introduction To UML
UML
What is UML?
Examples:
• System: Aircraft
• Models: Flight simulator, scale model
• Views: All blueprints, electrical wiring, fuel system
UML Models, Views, Diagrams
• UML is a multi-diagrammatic language
– Each diagram is a view into a model
• Diagram presented from the aspect of a particular stakeholder
• Provides a partial representation of the system
• Is semantically consistent with other views
– Example views
Models, Views, Diagrams
UML BASELINE
• Use Case Diagrams
• Class Diagrams
• Package Diagrams
• Interaction Diagrams
– Sequence
– Collaboration
• Activity Diagrams
• State Transition Diagrams
• Deployment Diagrams
UML Building Blocks
• Modeling Elements
– Structural
– Behavioral
– Organizational
– Annotational
• Diagrams that communicate
ideas using the modeling elements
• Views
Modeling Elements
• The above UML diagram shows that 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).
UML Class Diagram Example
• Now, let’s take what we’ve learned in the previous tutorial and apply it.
• In this example we are asked to create a class diagram for a banking
system. It must have the following classes:
– Bank
– ATM
– Customer
– Account
– Transaction
– Checking Account
– Savings Account
Determine Possible Class Members
Bank
• The bank class represents a physical bank.
• It has a location and a unique id.
• This bank also manages several accounts. **There’s an association!**
– What type of association is this?
– Is a bank entirely composed of accounts (composition)?
– Or are accounts ‘part of’ a bank (aggregation)?
– It looks like aggregation.
– It can’t be composition because that would mean that both classes live and die
together.
– That’s not quite right because you can have a bank without accounts and you can
have accounts without a bank.
• We’ll add a method called getAccounts().
ATM
• The ATM class represents a physical ATM.
• Right off the bat, we can come up with three methods for
the ATM:
– withdraw()
– deposit()
– checkBalance()
• Each of these methods takes the card number as input.
• In terms of attributes, an ATM has a location and is
managed by a specific bank.
Customer
• The customer class represents a real customer.
• This customer has a
– name
– address
– date of birth (dob)
– card number
– pin
• For this person to be considered a customer, they must have an
account. **There’s another association!**
– This isn’t aggregation or composition, it’s just a bi-directional association
(drawn using a blank line no arrows).
Account
• The account class represents a bank account.
• Common attributes of bank accounts include
– account number
– balance
– And more
• You can deposit() withdraw() money from the account.
• In addition, banks might offer two types of accounts:
– A checking account
– A savings account.
– These two can thus be considered child classes of the account class and can inherit from
it too.
– We’ll denote this by using a solid black line with an unfilled arrow going into the account
class.
Completed Diagram
Object Diagrams
• Shows instances of Class Diagrams and links among them
– An object diagram is a snapshot of the objects in a system
• At a point in time
• With a selected focus
– Interactions – Sequence diagram
– Message passing – Collaboration diagram
– Operation – Deployment diagram
• Format is
– Instance name : Class name
– Attributes and Values
– Example:
Objects and Links
SEQUENCE DIAGRAM
sequence diagram: an "interaction diagram" that models a single scenario executing in the
system
– perhaps 2nd most used UML diagram (behind class diagram)
• relation of UML diagrams to other exercises:
– CRC cards -> class diagram
– use cases -> sequence diagrams
Key parts of a sequence diag.
• participant: an object or entity that acts in the sequence diagram
– sequence diagram starts with an unattached "found message" arrow
• message: communication between participant objects
• the axes: in a sequence diagram:
– horizontal: which object/participant is acting
– vertical: time (down -> forward in time)
Sequence dg. from use case
Representing objects
• Squares with object type, optionally preceded by object name and colon
– write object's name if it clarifies the diagram
– object's "life line" represented by dashed vert. line
Messages between objects
39
Lifetime of objects
• creation: arrow with 'new'
written above it
– notice that an object created after
the start of the scenario appears
lower than the others
• deletion: an X at bottom of
object's lifeline
– Java doesn't explicitly delete
objects; they fall out of scope and
are garbage-collected 40
Indicating method calls
• activation: thick box over object's life line; drawn when
object's method is on the stack
– either that object is running its code, or it is on the stack waiting for
another object's method to finish
– nest to indicate recursion
Activation
Nesting
41
Indicating selection and loops
• frame: box around part of a sequence
diagram to indicate selection or loop
– if -> (opt) [condition]
– if/else -> (alt) [condition],
separated by horizontal dashed line
– loop -> (loop) [condition
or items to loop over]
42
linking sequence diagrams
• if one sequence diagram is too large or refers to another diagram, indicate it with either:
– an unfinished arrow and comment
– a "ref" frame that names the other diagram
– when would this occur in our system?
Approved?
43
Example sequence diagram
sd Example
loop
AddItem
ReserveItem
Checkout
ProcessOrder
ConfirmOrder
PlaceItemInOrder
44
Example sequence diagram
Example sequence diagram
Collaboration Diagram
• Collaboration Diagrams show similar information to sequence diagrams,
except that the vertical sequence is missing. In its place are:
– Object Links - solid lines between the objects that interact
– On the links are Messages - arrows with one or more message name that show the direction and
names of the messages sent between objects
• Emphasis on static links as opposed to sequence in the sequence diagram
• The collaboration diagram will indicate the objects participating in the
collaboration, and
• The sequence of messages {message(parameters), sender, receiver}
exchanged to achieve the task involved.
Collaboration Diagram
Collaboration Diagrams
Actor System
SystemOp
:ClassA
System Op
Operation:xxx
+ 1: message1()
Precondition:..
:ClassB
Contract Collaboration
Diagram
Use Case
Diagram
Collaboration Diagrams Notation
Illustrating links
sysOp() optional
1: tot := total():integer
:POST :Sale
sender receiver
Collaboration Diagrams
Illustrating Iteration
SystemOp( )
sender receiver
Sequence number followed by a Iteration clause is optional
* indicates iteration
Collaboration Diagrams
Illustrating Iteration
1: message( )
:ClassA
1b.1: msg5( )
:ClassC :ClassD
Collaboration Diagrams
New
1: create( )
c: Controller m:MenuItem
2: add(m)
:MenuItem
[order
accepted]
Send Accept
Invoice Make Payment
Invoice Payment
57
Activity Diagrams
• Fancy flowchart
– Displays the flow of activities involved in a single process
– States
• Describe what is being processed
• Indicated by boxes with rounded corners
– Swim lanes
• Indicates which object is responsible for what activity
– Branch
• Transition that branch
• Indicated by a diamond
– Fork
• Transition forking into parallel activities
• Indicated by solid bars
– Start and End
Some definitions
• Flow: permits the interaction between two nodes of the activity diagram
(represented by edges in activity diagram)
• State: a condition or situation in the life of an object during which it satisfies
some conditions, performs some activities, or waits for some events
• Type: specifies a domain of objects together with the operations applicable to
the objects (also none); includes primitive built-in types (such as integer and
string) and enumeration types
• Token: contains an object, datum, or locus of control, and is present in the
activity diagram at a particular node; each token is distinct from any other, even
if it contains the same value as another
• Value: an element of a type domain
59
Sample Activity Diagram
• Ordering System
• May need multiple
diagrams from
other points of view
Activity Diagram
Details:
-Start is a single circle
-End is a bulls-eye
-Decisions are diamonds
(guards must be on both
branches of the diamond!)
Activity Diagram Example
Swimlane Diagrams
66
Activities
• An activity is the specification of parameterized behaviour as the
coordinated sequencing of subordinate units whose individual
elements are actions
• Uses parameters to receive and provide data to the invoker
Activity
nodes
Parameter Activity Parameter Output
Input name
edges name
parameter Parameter parameter
name
68
Activity edges (1)
• Are directed connections
• They have a source and a target, along which tokens may flow
In this example we use a non-constant weight: an invoice for a particular job can
69
only be sent when all of its tasks have been completed
Activity edges (2)
• Two kinds of edges:
– Control flow edge - is an edge which starts an activity
node after the completion of the previous one by
passing a control token
Notation without
activity nodes
– Object flow edge - models the flow of values to or from
object nodes by passing object or data tokens
Notation without
activity nodes
70
Example – go to Genova Go to
Heaven/Hell ;)
Car crash
«local precondition»
Have a license
To Catch
Turn on [else] Fill up
motorway the
the car with fuel
[on car] tollgate ticket
[the tank is full]
Go to the
Buy the Obliterate Catch the
station with a
ticket the ticket train
friend
The train
Go to derail
Heaven/
Hell ;) The friend
goes home
When the train
arrives
to Genova
71
[Genova is a long way]