UML Sequence Diagram (1)

Source: Internet
Author: User

 

The main purpose of this article is to continue to focus on the basic UML diagram. This month, we will learn more about the sequence diagram. Note that the following example is based on the new UML 2 Specification.

Purpose

A Sequence Chart is used to display the interaction between objects in a series of order of interaction. Like a class chart, developers generally think that a sequence chart is only meaningful to them. However, the business staff of an organization sends
The sequence diagram shows how different Business Objects interact, which is useful for communicating with the current business. In addition to recording the current events of an organization, a business-level sequence chart can be used as a requirement file.
A future system transmission requirement. In the project demand phase, analysts can bring the use cases to the next level by providing a more formal expression. In that case, use cases are often refined into one or more orders.
Column chart.

The technical staff of the Organization can find that sequence diagrams are very useful in recording how a future system behave. In the design phase, architects and developers can use graphs to explore the interaction between system objects, so as to enrich the entire system design.

One of the main purposes of a sequence chart is to transform the Needs of the use case expression into further and more formal levels of fine expression. Use Cases are often divided into one or more sequence diagrams. In addition to designing new systems
They can also be used to record how objects in an existing system (called "Heritage") interact with each other. This document is useful when you hand over the system to another person or organization.

Symbol

Since this is the first article in my UML diagram series based on UML 2, we need to first discuss a supplement to the UML 2 diagram symbol, that is, a symbolic component called a framework. In
UML
In 2, frame elements are used as a basis for many other graph elements, but most people first come into contact with frame elements as graphical boundaries of the graph. When providing graphical boundaries for a graph, a framework Element
Provide consistent positions for the labels of the graph. In the UML diagram, the framework components are selectable, just as you can see in figures 1 and 2.
As you can see in, the label of the graph is placed in the upper left corner. In the namebox that I will call the framework, it is a kind of angle rectangle, in addition, the actual UML diagram is defined in a large enclosed rectangle.

Figure 1: Empty UML 2 framework component

In addition to providing a graphical border, framework components used in graphs also have important functions to describe interactions, such as sequence diagrams. In a sequence diagram, a sequence receives and sends messages (also known as interactions). A model can be established by connecting the message and the Framework Element Boundary (see figure 2 ). This will be described in more detail in the later section "Beyond basics.

Figure 2: A Sequence Diagram for receiving and sending messages

Note that in Figure 2, for a sequence chart, the label of the chart starts with the text "SD. When you use a frame element to close a graph, the labels of the graph must follow the following format:

Graph type graph name

UML specifications provide specific text values for graph types. (For example, SD represents a sequence diagram, activity represents an activity diagram, and use case represents a use case diagram ).

Basic
The main purpose of a sequence chart is to define the Event Sequence and generate expected output. The focus is not the message itself, but the order in which messages are generated; no
Most sequence diagrams indicate the messages passed between objects of a system and the order in which they occur. The figure transmits information in the horizontal and vertical dimensions: vertical dimensions indicate the message/call occurred from top to bottom
Time series, and the horizontal dimension from left to right indicates the object instance to which the message is sent.

Lifeline
When drawing a sequence diagram, place the lifeline symbol component across the top of the diagram. The lifeline indicates the role or object instance that is modeled in the sequence. 1 The lifeline painting is a square, with a dotted line from top to bottom passing through the center of the bottom boundary (Figure 3 ). The lifeline name is placed in the square.

Figure 3: An Example of the student class used for the lifeline of an object named freshman

The lifeline Naming Standard of UML follows the following format:

Entity name: Class Name

In the example 3, the lifeline represents the entity of the class student, and its object name is freshman. Note that the lifeline name is underlined. When underlines are used,
It means that the lifeline in a sequence diagram represents a specific entity of a class, not a specific entity of a specific type (for example, a role ). In a future article, we will learn about structured modeling. Now, only comment on the sequence diagram,
It may contain roles (for exampleBuyerAndSellerInstead of describing who plays those roles (for exampleBillAndFred). This allows repeated use of graphs in different contexts. Simple drag-and-drop: the Instance name of the sequence diagram is underlined, but the role name is not.

Figure 3
In this example, the lifeline is a named object, but not all lifeline objects represent named objects. On the contrary, a lifeline can be used to represent an anonymous or unnamed entity. In a sequence diagram,
When modeling an unnamed instance, the name of the lifeline follows the same pattern as that of a named instance. However, the location of the lifeline name remains blank, rather than providing an example image name. Refer to the figure again
3. If the lifeline is representing an anonymous example of the student class, the lifeline will be "student ". At the same time,
Because sequence diagrams are used in the project design phase, an unspecified object is completely legal: for example, "Freshman ".

Message
For readability, the first message in a sequence diagram always starts from the top and is usually located on the left of the diagram. Then the secondary message is added to the graph, which is slightly lower than the previous message.

To display an object (such as a lifeline) and send a message to another object, draw a line pointing to the receiving object, including a solid arrow.
(If it is a synchronous call operation) or a stick arrow (if it is an asynchronous signal ). The message/method name is placed on the line with an arrow. Message being passed to the receiving object, indicating the class of the receiving object
An operation/method. In the example in figure 4, the analyst object calls reportingsystem
Class. The analyst object calls the getavailablereports method of the system object. The system object then calls secsystem
The getsecurityclearance method on the object, including the userid parameter. The secsystem class type is securitysystem. 2

Figure 4: an instance that transmits messages between objects

In addition to displaying only the message calls in the sequence diagram, figure 4
The graph in also contains the returned message. These return messages are optional. A return message is drawn with a dotted line with an open arrow pointing back to the lifeline of the source. On this dotted line, you place the return value of the operation. In
Figure 4. When the getsecurityclearance method is called, The secsystem object returns userclearance
To the system object. When the getavailablereports method is called, The system object returns availablereports.

In addition, the returned message is an optional part of the sequence diagram. Returns the specific/abstract degree of dependency modeling for messages. It is useful to return a message if it needs to be more specific; otherwise, the active message is enough. I personally like to include a returned message whenever a value is returned, because I found that additional details make it easier to read a sequence diagram.

When modeling a sequence chart, sometimes an object needs to pass a message to itself. When is an object called itself? A purist will argue that an object should never pass a message to it.
Body. However, it may be useful in some situations to model an object that transmits a message to itself. For example, figure 5 is an improved version of Figure 4. Figure 5 version shows
Determineavailablereports
System Object of the method. The system transmits the message "determineavailablereports" to itself. The model focuses on the fact of the process, rather than the system
Image.

To draw a call object, such as what you do at ordinary times, draw a message, but not connect it to another object, but connect the message to the object itself.

Figure 5: The system object calls its determineavailablereports Method

The message instance in Figure 5 shows synchronous messages. However, you can also model asynchronous messages in the sequence diagram. An asynchronous message is similar to a synchronous method, but the line drawn by the message carries a single stick, as shown in figure 6.

 

Figure 6: sequence diagram fragment of asynchronous messages passed to entity 2

Constraints

When modeling the interaction of an object, messages are transmitted to the object only when a condition is met. Constraints are used in various UML diagrams for control flow. Here I will discuss UML
1. X and UML 2.0 constraints. In UML 1.x, a constraint can only be allocated to a single message. UML
In 1.x, to draw a constraint on a sequence chart, you place the constraint element on the constrained message line before the message name. Figure 7 shows a part of the sequence diagram, the message addstudent
Method has a constraint.

Figure 7: A segment in the UML 1.x sequence diagram, where the addstudent message has a constraint

In Figure 7, the constraint is the text "[pastduebalance = 0]". With the constraints on this message, if the accounting system returns an overdue balance of zero, the addstudent message will be passed. The constraint symbol is simple. The format is:

[Boolean Test]

For example,

[pastDueBalance = 0]

Combined fragments (variant scheme, selection items, and loops)
However, in most sequence diagrams
1. X "in-line" constraints are insufficient for the logic required to process a modeling sequence. This missing feature is a problem with UML 1.x. UML 2
This problem has been solved by removing the "in-line" constraint and adding a symbolic component called a combination fragment. A combined Shard is used to combine a set of messages and display the messages in a sequence diagram.
Component branch. The UML 2 Specification specifies the 11 combination fragments
Interaction Type. Three of the eleven types will be introduced in the "Basic" section, and the other two types will be introduced in the "beyond basic" section, the remaining six types will be introduced in another article. (Hi, this is
Rather than a book. I hope you can read this part in one day !)

Variant
A variant is used to specify the mutex selection between two or more message sequences. 3 variants support the classical "if then else" logic modeling (for example,IfI bought three,ThenI got a 20% discount on my purchase;OtherwiseI get a 10% discount for my purchase ).

As you will notice in figure 8, the combined fragment component of a variant is drawn using a frame. The word "Alt" is placed in the namebox of the framework. Then the larger rectangle is divided into the operation elements referred to as UML 2. 4. The operating elements are separated by dotted lines. Each operator element has a constraint for testing, which is placed on the left upper part of the operator element at the top of the lifeline. 5 if the constraint of the Operation element is "true", then the operation element is the operation element to be executed.

Figure 8: A Sequence diagram fragment that contains a variant combination Fragment

Figure
8. As an example of how to read the combined fragment of a variant, the sequence is displayed starting from the top, that is, the bank object gets the Check amount and account balance. In this case, the variant combinations in the sequence diagram take over. Because of constraints
"[Balance> = amount]". If the balance exceeds or equal to the amount, the Bank object is passed in order to adddebittransaction.
And storephotoofcheck
The message is sent to the account object. However, if the balance does not exceed or equal to the amount, the sequential process is that the bank transfers addinsuffientfundfee and
Notereturnedcheck message to account object, returncheck
Message to itself. Because of the "else" constraint, when the balance is not greater than or equal to the amount, the second sequence is called. In the combination fragment of a variant, the "else" constraint is not required. If an operation element,
There is no explicit constraint on it, so the "else" constraint will be assumed.

The combination fragments of variants are not limited to simple "if then else" verification. A large number of variant paths may be required. If you need more variants, all you have to do is add an operator element to a rectangle with sequence constraints and messages.

Select item
Select item Combination fragments for sequence modeling. These sequences give a specific condition that will happen; or, the sequence does not happen. A selection item is used to model a simple "If then" expression. (For example, if there are fewer than five circles on the shelf, make two more circles ).

The selected item Combination fragment symbol is similar to the variant combination fragment, except that it has only one operating element and never has an "else" constraint (it does so and has no reason ). Select a combination of items.
Frameworks. The text "Opt" is the text that is placed in the namebox of the framework. In the content area of the framework, the constraints of the selection items are placed in the upper left corner of the top corner of the lifeline.
Then, the message sequence of the selected item is placed in the rest of the content area of the framework. These elements 9 are shown.

Figure 9: A Sequence diagram segment that includes the selected item Combination Fragment

It is easy to read the selected item Combination fragments. Figure 9 is figure 7
But this time it uses a combination of selected item fragments, because if student's overdue balance is equal to 0, more messages need to be transmitted. See Figure 9
If student's overdue balance is equal to zero, then the addstudent, getcostofclass, and chargeforclass messages are passed. If
The expiration balance of student is not equal to zero. in the selected item Combination fragment, no message is transmitted in the sequence.

In Example 9, a sequence diagram segment contains a selection constraint. However, a constraint is not a required component. In a high-level and abstract sequence chart, you may not want to describe the conditions for selecting items. You may just want to point out that the clip is selectable.

 

 

Loop

Sometimes you will need to model a duplicate sequence. In UML 2, modeling for a recurring sequence has been improved, with a circular combination fragment attached.

The circular composite fragment surface is very similar to the selected item composite fragment. You can draw a framework and place the text "loop" in the namebox of the framework ". In the content area of the framework, at the top of a lifeline, the circular constraint 6 is
In the upper left corner. Then, the cyclic message sequence is placed in the rest of the framework content area. In a loop, apart from standard Boolean tests, one constraint can test two specific conditional statements. Specific constraints
The formula is the minimum number of cycles for writing "MININT = [The number]" (for example, "MININT = 1"), and for writing "maxint = [
Number] "(for example," maxint =
5. Through the minimum cycle test, the cycle must run at least a specified number of times, and the number of cycles cannot reach the maximum number of cycles specified by the constraint.

Figure 10: A Sequence Chart example of cyclic composite fragments

The circular operation shown in Figure 10 continues until the hasanotherreport of the reportsenu object
The message returns false. If the loop sequence should run, the loop of this sequence diagram uses a Boolean test to confirm. To read this picture, you start from the top, just as you normally do. When you reach the cyclic combination fragment,
Perform a test to check whether the value of hasanotherreport is true. If hasanotherreport
The value is equal to true, so the sequence enters the loop segment. Then, you can track cyclic messages in a sequence diagram, just like normal.

Beyond basic

I have already introduced the basics of sequence diagrams, which should allow you to model most of the interactions that will normally occur in the system. The following section describes the higher-order symbolic elements used for sequence diagrams.

Reference another sequence diagram
When using sequence diagrams, developers love to reuse existing sequence diagrams in their sequence diagrams. 7.
In UML 2, the "interactive" component is introduced. The append interaction can be called UML 2.
The most important innovation in interactive modeling. The original sequence diagram is organized into a complex sequence diagram. Because of these, you can combine (reuse) Simpler sequences to generate more complex sequences. This
It means you can abstract a complete, possibly complex sequence into a single conceptual unit.

An interactive component is drawn using a framework. The text "Ref" is placed in the namebox of the framework, and the referenced sequence diagram name is placed in the content area of the framework, together with any parameters of the sequence diagram. The following schema references the name symbols of a sequence chart:

Sequence diagram name [(parameter)] [: return value]

Two examples:

1. Retrieve borrower credit report (SSN): borrowercreditreport

Or

2. Process Credit Card (name, number, expirationdate, amount: 100)

In example 1, the syntax calls a sequence diagram called retrieve borrower credit report and passes it to the SSN parameter. The returned variable borrowercreditreport is returned for the Series retreive borrower credit report.

In instance 2, the syntax call is called process credit.
Name, number, expiration date, and amount. However, in example 2
The value of amount is 100. Because Example 2 does not have a return value label, and the sequence does not return values (assuming that the modeling sequence does not need to return values ).

Figure 11: A Sequence diagram referencing two different sequence diagrams

Figure 11 shows a sequence diagram that references the sequence diagrams "balance lookup" and "debit
Account ". The sequence starts from the upper left corner and the customer passes a message to the teller object. The teller object transmits a message to theirbank.
Object. At that time, the balance lookup sequence diagram was called, and the accountnumber was passed as a parameter. Balance
The lookup Sequence Chart returns the balance variable. Then, check the constraints of the selected item Combination fragment and confirm that the balance is greater than the amount variable. Call debit when the balance is greater than the amount.
Account sequence diagram, and pass the accountnumber and amount parameters to it. After that sequence is complete, withdrawcash
The message returns cash for the customer.

It is important to note that in Figure 11, theirbank's lifeline is interactive for balance
Lookup is hidden. Because the lifeline of interaction is hidden, the lifeline of theirbank is
Lookup "is referenced in the sequence diagram. In addition to hiding the lifeline of interaction, UML 2 also specifies that the lifeline is in its own "balance"
The lookup sequence must have the same theirbank.

Sometimes, you model a sequence chart, where interactions overlap.NoThe lifeline referenced in the interaction. In that case, the lifeline is displayed like a normal lifeline and will not be hidden by overlapping interactions.

In Figure 11, the sequence references the "balance lookup" sequence diagram. The "balance lookup" sequence diagram is shown in Figure 12. Because the sample sequence has a parameter and a return value, its label -- located in the namebox of the graph -- follows a specific pattern:

Graph type graph name [parameter type: parameter name]
[: Return Value Type]

Two examples:

1. SD balance Lookup (INTEGER: accountnumber): Real

Or

2. SD available reports (financial analyst: analyst): Reports

Figure 12 illustrates Example 1. In this example, the balance lookup sequence uses the accountnumber parameter as the variable in the sequence, and the sequence diagram shows the returned real object. In this case, the returned object uses the sequence diagram entity name.

Figure 12: A sequence chart that uses the accountnumber parameter and returns a real object

Figure 13 illustrates Example 2. In a sequence diagram, a parameter is obtained and an object is returned. However, parameters in Figure 13 are used in sequence interaction.

Figure 13: a sequence chart that uses parameters in its interaction and returns a Reports object

Portal
The previous section shows how to pass information through parameters and return values and reference another sequence diagram. However, there is another way to transmit messages between sequence diagrams. The door may be an easy way,
Modeling of passing messages between a sequence diagram and its context. A portal is just a message, and the image is represented as the frame edge connecting one end to the sequence diagram, and the other end to the lifeline. Figure 11 and 12 using the door
You can see the refactoring in figures 14 and 15. In the example in Figure 15, a portal called getbalance is provided to obtain parameters.
Accountnumber. Because the arrow line is connected to the graph frame, and the arrow is connected to the lifeline, getbalance
A message is an entry gate. A sequence chart also has an exit gate and returns the balance variable. The same can be seen at the exit because it is a returned message that connects the frame from a lifeline to the graph and connects the arrow to the frame.

Figure 14: rebuild in Figure 11.
Figure 15: rebuild in Figure 12.

 

 

Combined fragments (jump and parallel)
As described in the previous section "Basic", I introduced the combined fragments of "variants", "selection items", and "loops. Most people will use the most of these three combinations. However, there are two other combinations of fragments, a large number of people will find useful-jump and parallel.

Jump
The hop combination fragment is consistent with the selected item Combination fragment in almost every aspect, except for two exceptions. First, the jump Frame
The namebox text "break" replaces "option ". Second,
When a message that jumps to the combined Shard is running, other messages with closed interaction will not be executed because the sequence breaks the closed interaction. In this way, the jump composite fragments are very similar to C ++ or Java
The break keyword in the programming language.

Figure 16: reconstruction of the sequence diagram segment from figure 8, where the segment uses jump instead of Variant

Redirection is most commonly used for model exception handling. Figure 16 is the reconstruction of figure 8, but this time figure 16 uses the jump combination fragments because it puts the balance <
Amount is treated as an exception rather than a variant stream. To read the Graph
16. You start from the upper left corner of the sequence and read down. When the sequence reaches the return value "balance", it checks to see if there is less balance than the amount. If the balance is not less than the amount, the next consumption is passed.
The parameter is adddebittransaction.
Message, and the sequence continues normally. However, when the balance is less than the amount, and then the sequence goes into the jump combination fragment, its message is transmitted. Once the messages in the jump combination have been transmitted, the sequence will not send any
Other messages exit (for example, adddebittransaction ).

Note that one important thing about redirection is that they only cause a closed interaction sequence to exit and do not need to complete the sequence described in the figure. In this case, the jump combination is a variant or part of a loop, and then only the variant or loop is exited.

Parallel
Today's modern computer systems are constantly evolving in complexity and sometimes concurrent tasks. When it takes longer to complete a complex task than expected, some systems adopt parallel processing of various parts of the process. When you create a sequence chart to display parallel processing activities, you need to use parallel combination of fragment components.

Parallel combination fragments are drawn using a framework. You put the text "par" in the namebox of the framework. Then you divide the content section of the Framework into horizontal operation elements by dotted lines. Each operating element of the framework represents a thread running in parallel.

Figure 17: oven is an object instance that performs two tasks in parallel.

Figure 17
There may be no examples of the best computer system instance for objects that do parallel activities, but an example of a series of parallel activities that is easy to understand. The sequence is as follows: hungryperson
Pass the cookfood message to the oven object. When the oven object receives the message, it sends two messages (nukefood and
Rotatefood) to itself. After these messages are processed, the hungryperson object returns yummyfood from the oven object.

Summary
A sequence diagram is a good diagram used to record system requirements and organize system design. The reason why a sequence chart is so useful is that it displays the interaction logic between objects in the system according to the time sequence of interaction.

Reference

  • UML 2.0 superstructure final adopted specification (Chapter 1) http://www.omg.org/cgi-bin/doc? PTC/2003-08-02
  • UML 2 sequence dimo-overview http://www.agilemodeling.com/artifacts/sequenceDiagram.htm
  • UML 2 tutorial http://www.omg.org/news/meetings/workshops/UML%202003%20Manual/Tutorial7-Hogg.pdf

Footer
1. In a fully modeled system, the object (class instance) will also be modeled in the class diagram of the system.

2. When reading this sequence diagram, assume that the analyst logs on to the system.

3. Note that two or more constraints and conditions attached to different variant operators may be true at the same time, however, only one operating element will actually occur at most during running (in that case, the variant "wins" is not defined according to the UML standard ).

4 although the operator yuan looks like a path on a road, I don't call them a path. A swimming track is a UML symbol used in an activity diagram. SeeThe rational edgeEarly articles on activity diagrams.

5. Generally, the lifeline of the constraint attached is the lifeline that owns the variables contained in the constraint expression.

6. For the selected item Combination fragment, you do not need to place a constraint on the cyclic combination fragment.

7. Any type of Sequence Diagram (for example, program or business) may be reused ). I just found that developers prefer to break down their diagrams by function.

For more information, see the original article on the developerworks global site.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.