State Diagrams
State Diagrams
State Diagrams
In next lessons you'll work with elements that you haven't worked before. That would
be behavioral elements, that show how parts of a UML model change over time. As the
system interacts with users and possibly with other systems, the objects that make up the
system go through necessary changes to acommodate the interactions. If you're going to
model systems, you must have a mechanism to model change. That mechanism in UML is
State diagrams.
- When you pull a switch, a light changes its state from off to on.
- When you make keystroke or mousemovement in screen saver mode on your computer,
your computer leaves screen saver mode and returns to working - active mode.
The UML State diagram presents the states an object can be in along with the transitions
between the states, and shows the starting point and endpoint of a sequence of state
changes.
A state diagram shows the states of a single object.
States in the state diagram are represented with a rounded rectangle, and the symbol for the
transition is a solid arrowhead line. A solid circle stands for starting point of a sequence of
states, and bull's eye represents the endpoint.
Some of the states may be more complex that is represented with a rounded rectangle. It is
possible to have states inside one state. These states are called substates (sequential and
concurrent).
Sequential substates come in sequences of states which occur one after another.
Concurrent substates must consist of two or more sequential substates which occur at the
same time. This is represented with dotted line between the concurent states.
If you have any problems understanding these concepts please refer to the State
Diagram - Example lesson.
The UML supplies a symbol that shows that a composite state remembers its active substate
when the object transitions out of the composite state. The symbol is
connected by a solid
line to the remembered substate, with an arrowhead that points to the substate.
A message that triggers a transition in the receiving object's state diagram is called a signal.
In the object-oriented world, sending a signal is the same as creating a signal object and
transmitting it to the receiving object. The signal object has properties that are represented
as attributes. Because a signal is an object, it's possible to create inheritance hierarchies of
signals.
Sequence Diagrams
In previous lesson we learned state diagrams, which focus on the states of na object. But it's
necessary to have communication between objects. The UML sequence diagram goes the
next step and shows how objects communicate with one another over time. In this expanded
field of view, you'll include an important dimension: time. The key idea here is that
interactions among objects take place in a specified sequence, and the sequence takes time
to go from beginning to end.
The sequence diagram consists of objects represented in the usual way - as named
rectangles (with the name underlined), messages represented as solid-line arroes,
and time represented as a vertical progression.
Let's take a close look at this parts of sequence diagrams.
Objects
The objects are laid out near the top of the diagram from left to right. They're arranged in any
order that simplifies the diagram. Extending downforward from each object is a dashed line
called the object's lifeline. Along the lifeline is narrow rectangle called an activation. The
activation represents an execution of an operation the object carries out. The length of the
rectangle signifies the activation's duration. This is shown on next picture.
The actor-symbol initiates the sequence, but the stick figure isn't part of the sequence
diagram symbol set.
In a sequence diagram, the objects are laid out from left to right across the
top. Each object' lifeline is a dashed line extending downward from the object. A solid
line with an arrowhead connects one lifeline to another, and represents a message
from one object to another. Time starts at the top an proceeds downward. Although an
actor typically initiates the sequence, the actor symbol isn't part of the sequence
diagram's
symbol set.
Ways
of Creating Sequences
As use case diagram can show either an instance (one scenario) of a use case, or it can be
generic and incorporate all of the use case's scenarios, and sequence diagrams can
beinstance or generic sequence diagrams. Generic sequence diagrams often provide
opportunities to represent if statements and while loops. Enclose each condition for an "if"
statement in square brackets. Do the same for the condition that satisfies a "while" loop, and
prefix the left bracket with an asterisk. See Sequence Diagram - Example.
It's possible in sequence diagrams to create an object. When this occurs, you represent a
created object in the usual way - as a named rectangle.
The difference is that you don't position it at the top of the sequnce diagram as you do with
the other objects. Instead, you position it along the vertical dimension so that its location
corresponds to the time when it's created. The message that creates the object is
labeled Create().
Sometimes an object has an operation that invokes itself. This is
called recursion.
Suppose one of the objects in your system is a calculator, and suppose one of its operations
computes interest. In order to compute compound interest for a timeframe that
encompasses several compouding periods, the object's interest-computation operation has
to invoke itself a number of times. Sequence diagram for this is following:
the dispenser
lifeline, separating the lifeline into separate paths. At some point in the sequence, the
branches in the message merge, as do the forks in the lifeline.
For the incorrect-amount-of-money scenario, the list of sequences is given in the following
table:
1
2
3
4
5
The register checks customer's input with the price of the product
If the amount is greater then the price, the difference is calculated, and register
checks its cash reserve
If the difference is present in the cash reserve, the register returns the change to the
customer and everything proceeds as before
Otherway, the register returns the input amount and displays a message that prompts
the customer for the correct amount
If the amount is less than the price, the register does nothing and the machine waits
for more money
Collaboration Diagrams
Collaboration diagrams like the sequence diagrams, shows how objects interact. It shows
the objects along with the messages that travel from one to another. But, if sequence
diagram does that, why UML need another diagram? Don't they do the same thing?
The sequence diagram and the collaboration diagram are similar. They're semantically
equivalent, that is, the present the same information, and you can turn a sequence to a
collaboration diagram and vice versa. The main distinction between them is that the
sequence diagram is arranged according to time, the collaboration diagram according to
space.
A collaboration diagram is an extension of object diagram. In addition to the
associations among objects, collaboration diagram shows the messages the objects send
each other.
Messages among objects are represented with arrows that points to receiving object, near
the association line between two objects. A label near the arrow shows what the message is.
The message typically tells the receiving object to execute one of its operations. A pair of
parentheses ends the message. Inside the parentheses, you put the parameters the
opearation works on.
Collaboration diagrams can be turned into sequence diagrams and vice versa.
Thus, you have to be able to represent sequence information in a collaboration diagram. To
do this you must know that:
Add a number to the label of a message, with the number corresponding to the
message's order in the sequence. A colon separates the number from the
message.
This is the basis of collaboration diagrams. In addition next two lessons will describe the
process of writing collaboration diagrams.
The customer inserts money in the machine and makes selection of one or more
product(s) present in the machine.
When the register gets the money (in this case customer puts the correct amount of
money, and there is allways a product in the selected brand), the selected product is
delivered to the dispenser.
The dispenser delivers the product to the front of the machine, and the customer gets
that product.
But when we examined this machine and its work in detail, new scenarios were introduced:
incorect-amount-of-money and out-of-product scenario. While building the sequence
diagrams, conditions were introduced to display the work of the machine. Here conditions
will be used too.
You put the condition inside a pair of square brackets, and the condition precedes the
message-label. The important thing is to coordinate the conditions with the numbering.
When you're modeling if statement there are two possible conditions. Messages for these
conditions have a same number, and you add a decimal point and another number. This is
called nesting.
In out-of-product scenario, the machine has to display an out of product
message, and prompt the customer for another product or return the money back. If
customer makes another selection this process must be repeated. Also a correct-amount-ofmoney scenario may be introduced. Collaboration diagram for this case is shown on the
picture:
As you can see, from this picture two possible scenarios may happen. Out-ofproduct and Product-in are that two possibilities. If we assume that this happened after
message1:add(input,selection), the number for this messages will be 2, and we add 1 for the
Product-in and 2 for the out-of-product scenarios, after the decimal point.
What happens when the machine doesn't have the correct change? The machine has to
display an out of change message, return the money, and prompt the customer for the
correct change. In effect, the transaction is over. When the machine has the correct change,
it returns the change to the customer and delivers selected product.
Two branches that are produced from nesting process are numbered with 2.1 and 2.2. And
three posibilities in condition-message 3 will be:3.1, 3.2 and 3.3. Collaboration diagram is
shown on the following picture:
Thats all for this example, and for this day. All you have to do now is answering the
questions given in Questions & Answers and Workshop part from this day. Next day will
introduce to you the last three UML diagrams.
Activity Diagrams
If you've ever taken an introductory course in programming, you've probably encountered
the flowchart. The flowchart shows a sequence of steps, processes, decision points and
branches. Novice programmers are encouraged to use flowcharts to conceptualize problems
and derive solutions.
The UML activity diagram is much like the flowcharts of old. It shows steps (called,
appropriately enough, activities) as well as decision points and branches. It's useful for
showing what happens in a business process or an operation. You'll wind it an integral part
of system analysis.
First and foremost, an activity diagram is designed to be a simplified look at what happens
during an operation or a process. It's an extension of the state diagram. The state diagram
shows the states of an object and represents activities as arrows connecting the states. The
activity diagram highlight the activities.
Each activity is represented by a rounded rectangle - narrower and more oval-shaped than
the state icon. The processing within an activity goes to completion and then an automatic
transmission to the next activity occurs. An arrow represents the transition from one activity
to the next. Also an activity diagram has a strating point represented by filled-in circle, and
endpoint represented by a bull's eye.
The activity diagram is an extension of the state diagram. State diagrams highlight
states and represent activities as arrows between states. Activity diagrams put the
spotlight on the activities. Each activity is represented as a rounded rectangle, more
oval in appearance than the state icon. The activity diagram uses the same symbols
as the state diagram for the startpoint and the endpoint.
If you're dealing with computer programming you will conclude that this activity diagram is
very similar with a procedure (function) that computes factoriels.