UML Sequence and Object diagrams

Just add the images and you are fine -all you need to know-

article image

UML Sequence Diagrams

Used for: Represent relationship between several entities in time

docs: Docs

basics:

In here we create three entities, with the two basics kind of arrows.

@startuml
Alice -> "Bob()" : Hello
"Bob()" -> "This is very\nlong" as Long
' You can also declare:
' "Bob()" -> Long as "This is very\nlong"
Long --> "Bob()" : ok
@enduml

Message to self

Easy peasy, just the same name as emitter and receptor:

@startuml
Alice->Alice: This is a signal to self.\nIt also demonstrates\nmultiline \ntext
@enduml

Failed arrow

@startuml
Bob ->x Alice
@enduml

Arrow color

@startuml
Bob -[#red]> Alice : hello
Alice -[#0000FF]->Bob : ok
@enduml

Header and title

@startuml

header Page Header

title Example Title

Alice -> Bob : message 1
Alice -> Bob : message 2

@enduml

Notes

Notes at the left or right of the messages

@startuml
Alice->Bob : hello
note left: this is a first note

Bob->Alice : ok
note right: this is another note

Bob->Bob : I am thinking
note left
a note
can also be defined
on several lines
end note
@endum

You can put notes over the lines of one entity -or both- as well (and with different colours!!!)

@startuml
participant Alice
participant Bob
note left of Alice #aqua
This is displayed
left of Alice.
end note

note right of Alice: This is displayed right of Alice.

note over Alice: This is displayed over Alice.

note over Alice, Bob #FFAAAA: This is displayed\n over Bob and Alice.

note over Bob, Alice
This is yet another
example of
a long note.
end note
@enduml

And put them at the same level with "/"

@startuml
note over Alice : initial state of Alice
/ note over Bob : initial state of Bob
Bob -> Alice : hello
@enduml

Space, use "|||"

You can specify the number of pixels as well:

@startuml

Alice -> Bob: message 1
Bob --> Alice: ok
|||
Alice -> Bob: message 2
Bob --> Alice: ok
||45||
Alice -> Bob: message 3
Bob --> Alice: ok

@enduml

Create a new entity

You can use the keywork create to emphasize that a message creates a new object

@startuml
Bob -> Alice : hello

create Other
Alice -> Other : new

create control String
Alice -> String
note right : You can also put notes!

Alice --> Bob : ok

@enduml

Vertical arrows between messages {start} <-> {end}

@startuml
!pragma teoz true

{start} Alice -> Bob : start doing things during duration
Bob -> Max : something
Max -> Bob : something else
{end} Bob -> Alice : finish

{start} <-> {end} : some time

@enduml

Box for participants

@startuml

box "Internal Service" #LightBlue
participant Bob
participant Alice
end box
participant Other

Bob -> Alice : hello
Alice -> Other : hello

@enduml

cool complicated example: passport for express.js

@startuml

participant User as "User Browser"
participant Server
participant Session as "express-session middw"
participant passauth as "passport.authenticate() middw"
participant passini as "passport.initialize() middw"
participant passsess as "passport.session() middw"
participant db
note over User #FFAAAA
user connects for the
first time to this domain
end note

/ note over passini, passsess #silver
both will look in every request for
req.session.passort, if it doesn't exists
it is ignored in the diagram
end note

User -> Server: GET request to /login

Server -> Session : ey, there is a connection to the server
note over Session
checks Cookie HTTP header
on the req obj, as it is the
first time, there is none
end note

Session -> db : create session record on db
Session -> Session : creates req.session,\ncreates the cookie for the session\nand puts it in req.session


Session -> Server : here is the Cookie with the session but no user Id 


Server -> User : html for the login form\nwith the Cookie in the header

note over User #FFAAAA
the user gets bored
and closes the laptop
end note

note over User #aqua
the user comes back
and connects to login
end note

User -> Server : GET request to /login
Server -> Session : hey! there is another connection to server

note over Session
checks Cookie HTTP header
on the req obj, and this time
it finds one -the one created
before-
end note

note over Session
checkd the connect.sid value from the cookie
end note

Session -> db : does this session exists\nin the db?
db -> Session : yes
Session -> Server : everything cool,\nnothing to change here
Server -> User : here is the form again\nand the same cookie in\n the header as before

User -> Server : here is my username and password (POST to /login)

Server -> passauth : this route uses the\npassport.authenticate() middleware
passauth -> db : are this username and password in the db? (we assume they are)
db -> passauth : yes

note over passauth : cb returns user
note over passauth : it creates passport property in req.session
note over passauth : serializes the user via passport.serializeUser()
note over passauth : attach serialized user (for example the user id) to req.session.passport.user
note over passauth : attach the whole user to req.user

passauth -> Server : we are all set
Server -> User : HTML with message "welcome back!"

User -> Server : GET to authenticate route

Server -> Session : hey! another connection to server!
note over Session: check Cookie from req obj (connect.sid)
Session -> db : does this session exists?
db -> Session : yes
note over Session: cool, we don't change Cookie then

Session -> passini : next (all request go through this two middlewares\nbut don't do shit because there is\nnot req.session.passport property\n, but this time is different)

note over passini
checks req.session.password.user and see that there
is a serialized user there -probably user id-
end note

passini -> passsess: next -and pass the serialized user-
passsess -> db: give me the user for this userid
db -> passsess: there it is
note over passsess: set the user to req.user again

passsess -> Server: we are all set
Server -> Server: the protected route checks that req.session.passport.user\nexists -it does, as we just set it-\nand works fine

Server -> User: here is your HTML and we are cool

@enduml

article image

UML Object Diagram

Used to represent relations between objects

docs: Docs

Create Objects (and give an alias)

@startuml
object firstObject
object "My Second Object" as o2
@enduml

Adding fields

@startuml

object user

user : name = "Dummy"
user : id = 123

@enduml

or another -and easier- way of doing it:

@startuml

object user {
name = "Dummy"
id = 123
}

@enduml

Maps

@startuml
map CapitalCity {
UK => London
USA => Washington
Germany => Berlin
}
@enduml

Relationships between maps

Link a fields to another object -check out the length of the arrows-

@startuml
object London
object Washington
object Berlin
object NewYork

map CapitalCity {
UK *-> London
USA *--> Washington
Germany *---> Berlin
}

NewYork --> CapitalCity::USA
@enduml

Relations between objects -check the length of the arrows-

@startuml
object London {
country: uk,
population: 8M
}

object Seville {
country: spain,
population: 1M
}

object Petersfield {
country: uk,
population: 0.05M
}

London --> Seville
London ----> Petersfield
@enduml

Don't be shy, leave us a comment