UML sequence diagram

Source: Internet
Author: User

It's now February, And now you may have read or heard people talk about the changes made to UML 2.0, a new specification that includes several progressive UML. Considering the importance of the new specification, we are also changing the basis of this article series to focus our attention on the omg uml 1.4 specification, transferred to the OMG has incorporated the UML 2.0 draft Specification (also known as UML 2 ). I do not like to change the focus from 1.4 to 2.0 in the middle of a series of articles. However, the standardization of the UML 2.0 draft is an important step forward, and I feel that it is necessary to expand the text.

For some reason, OMG improved UML. The basic reason is that they want UML models to express the model-driven architecture (MDA), which means that UML must support many other model-driven symbols. At the same time, the UML 1.x symbol set is sometimes difficult to apply to large applications. In addition, in order to make the image easier to read, you need to improve the symbol element. (For example, the logical process of the UML 1.x Model is too complex and sometimes impossible to complete. Significant progress has been made in serialization logic modeling due to changes in the symbol set of the sequence diagram in UML 2 ).

Note the text I mentioned above: "Draft uml2.0 specifications have been accepted ." Indeed, the specification is still in the draft State, but the key is that the draft Specification has been used by OMG, And OMG is an organization that will not be used until the new standard is quite reliable. There will be some changes to the specification before UML 2 is fully applied, but these changes should be minimal. The basic change will be within UML-including the functionality that is typically used by software companies that implement UML tools.

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 sample 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 personnel of an organization will find that the sequence diagram shows how different Business Objects interact and is very 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 to meet future system transfer requirements. 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 subdivided into one or more other sequence diagrams.

The technical staff of the Organization can find that the sequence diagram is very useful in recording the behavior of a future system. In the design phase, architects and developers can use diagrams 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 subdivided into one or more other sequence diagrams. In addition to the purpose of designing a new system, sequence diagrams can also be used to record how objects in an existing system (called a legacy) Interact today. This document is very 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 2, frame elements are used as a basis for more graph elements, but most people first come into contact with frame elements as graphical boundaries of graphs. When a graphic border is provided for a graph, a frame element provides a consistent position for the labels of the graph. In a UML diagram, the framework components are selectable. As you can see in figures 1 and 2, the labels of the diagrams are placed in the upper left corner, in the "namebox" of the framework that I will call, it is a kind of angle rectangle, and the actual UML diagram is defined inside a large closed rectangle.

Figure 1: Empty UML 2 framework component

In addition to providing a graphical border, the framework components used in graphs also have important functions to describe interaction, 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 detailed in the subsequent section "beyond the basics.

Figure 2: A Sequence Diagram for receiving and sending messages

Note that in Figure 2, for a sequence diagram, the labels of the diagram Start with the text "SD. When you use a frame element to close a graph, the label 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 on the message itself, but on the order in which messages are generated. However, most sequence diagrams indicate what messages are transmitted between System Objects and the order in which they occur. The figure transmits information in the horizontal and vertical dimensions: the vertical dimension indicates the time sequence of the message/call occurrence from top to bottom, and the horizontal dimension indicates the object instance to which the message is sent from left to right.

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: Example of a 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 shown in 3, the lifeline represents the entity of the class student, and its object name is freshman. Note that the lifeline name is underlined. When an underline is used, it means that the lifeline in a sequence diagram represents a specific entity of a class, not a specific entity (such as a role ). In a future article, we will learn about structured modeling. Now, only comment on the sequence diagram, which may include roles (suchBuyerAndSellerInstead of describing who plays those roles (for exampleBillAndFred). This permits 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.

In Figure 3, the lifeline sample is a named object, but does all the lifeline represent the named object. On the contrary, a lifeline can be used to represent an anonymous or unnamed entity. When a sequence chart is modeled for an unnamed instance, the lifeline name scheme is in the same pattern as a named instance. However, the location of the lifeline name remains blank, instead of providing an example image name. Test Figure 3 again. Assume that the lifeline is representing an anonymous example of the student class, And 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 add the secondary message? In the figure, the message is slightly lower than the previous one.

To display an object (such as a lifeline) and send a message to another object, you draw a line pointing to the receiving object, which contains a solid arrow (assuming it is a synchronous call operation) or a single arrow (assuming an asynchronous signal ). The message/method name is placed on the line with an arrow. The message being passed to the receiving object, indicating an operation/method implemented by the class of the receiving object. In the example in figure 4, the analyst object calls the system object of an instance of the reportingsystem class. The analyst object calls the getavailablereports method of the system object. The system object then calls the getsecurityclearance method on the secsystem object that contains the userid of the number of shards. 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, the diagram in Figure 4 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 detailed/abstract degree of the message dependency modeling. If you need better details, it is practical to return the message; otherwise, the active message is enough. I personally like it. No matter when a value is returned, it contains a returned message. I found that extra details make it easier to read a sequence chart.

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 itself. However, modeling for an object that transmits a message to itself may be practical in some situations. For example, figure 5 is an improved version number in Figure 4. Figure 5 version number shows the system object that calls its determineavailablereports method. The system transmits the message "determineavailablereports" to itself. The model focuses on the process rather than the system object.

To draw a call object, such as what you do at ordinary times, you can draw a message to connect it to another object. Instead, you can 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, which is 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 the constraints of UML 1.x and UML 2.0. In UML 1.x, a constraint may only be assigned to a single message. In UML 1. X, to draw a constraint on a sequence diagram, you place the constraint element on the constrained message line before the message name. Figure 7 shows a part of the sequence diagram, which has a constraint on the message addstudent method.

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]". Based on the constraints on this message, if the accounting system returns a zero-point balance overdue, the addstudent message will be passed. The constraints are very easy. The format is:

[Boolean test]

For example,

[Pastduebalance = 0]

Combined fragments (variant scheme, selection items, and loops)
However, in most sequence diagrams, the UML 1.x "in-line" constraint is insufficient to process the required logic of a modeling sequence. This missing feature is a problem with UML 1.x. UML 2 has been added by removing the "in-line" constraint? A symbolic component called a combination fragment solves this problem. A combined Shard is used to combine a set of messages and display the condition branches in a sequence diagram. The UML 2 Specification specifies 11 types of interaction for combined fragments. 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 an article, not a book. I hope you can read this part in one day !)

Variant
A variant is used to specify the mutually exclusive choice between two or more other message sequences. 3 variants support the classical "if then else" logic modeling (for example,HypothesisI 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 the Token test, which is placed on the left upper part of the operator element at the top of the lifeline. 5. Assume that the constraint of the Operation element is "true", and that operation element is the operation element to be run.

Figure 8: A Sequence diagram segment that includes a variant combination Fragment

Figure 8 shows an example of how to read the combined shard of a variant. The sequence starts from the top, that is, the bank object obtains the Check amount and account balance. In this case, the variant combinations in the sequence diagram take over. Due to the constraint "[balance> = amount]", assuming that the remaining amount exceeds or equal to the amount, the Bank object is then delivered with the adddebittransaction and storephotoofcheck messages to the account object in sequence. However, if the remaining amount is not greater than or equal to the amount, then the sequential process is that the Bank transmits the addinsuffientfundfee and notereturnedcheck messages to the account object, and the returncheck message to itself. Due to the "else" constraint, when the remaining amount is not greater than or equal to the amount, the second sequence is called. In the combination fragment of variants, there is no need for the "else" constraint. If an operator element does not have a clear constraint on it, then 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 operation? In 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, then there are two more circles ).

The selected item Combination fragment symbol is similar to the variant combination fragment, except that it only has an operator element and never has an "else" constraint (it does so and has no reason ). You need to draw a selection item Combination and draw a frame. 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 See.

Figure 9: A Sequence diagram fragment containing the selected item Combination Fragment

Reading the selected item Combination fragments is very easy. Figure 9 is the re-processing of the sequence diagram fragment in Figure 7. However, this time it uses a combination of fragments of a selection item. Since student's overdue balance is equal to 0, many other messages need to be transmitted. According to the sequence diagram in Figure 9, if the overdue balance of student is equal to zero, and then the overdue balance of addstudent, getcostofclass, and chargeforclass messages h between them is not equal to zero, then in the selected item Combination fragment, no matter what message is transmitted in a sequence.

The sequence diagram segment in Figure 9 contains a selection constraint. However, the 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 recurring sequence. In UML 2, an iterative sequential modeling has been improved, and cyclic composite fragments are attached.

The surface of the cyclic composite fragment is similar to that of the selected 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, circular constraint 6 is placed 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 the standard Boolean condition test, one constraint can test two specific conditions. The specific constraint condition is the minimum number of cycles for writing "MININT = [The number]" (for example, "MININT = 1, and the maximum number of cycles for writing "maxint = [The number]" (for example, "maxint = 5. Through the minimum cycle test, the loop must be executed for at least a specified number of times, but the number of cycles cannot reach the maximum number of cycles specified by the constraint.

Figure 10: A Sequence Chart example of the cyclic composite fragment (click to zoom in)

The circular execution shown in Figure 10 is executed until the hasanotherreport message of the reportsenu object returns false. Assume that the cyclic sequence should be executed, and the loop of this sequence diagram uses a Boolean sequence for trial confirmation. To read this picture, you start from the top, just as you normally do. When you reach the combined parts of the loop, perform a testing to check whether the value of hasanotherreport is true. Assuming that the hasanotherreport value is equal to true, 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, so you should be able to model most of the interactions that will normally occur in the system. The following sections describe the symbol elements that are higher than the limit level for a sequence chart.

Reference another sequence diagram
When using sequence diagrams, developers love to reuse existing sequence diagrams in their sequence diagrams. 7. Start with UML 2 and introduce "interactive" components. Append interaction is the most important innovation in UML 2 interaction modeling. Add interaction? The original sequence diagram is organized into a complex sequence diagram. Because of this, you can combine (reuse) simple sequences to generate sequences that are more complex than the sequences. This means that you can abstract a complete, possibly more complex sequence than a 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 the number of sequences of the sequence diagram. The name symbol of the referenced sequence chart is in the following mode:

Sequence diagram name [(number of sequences)] [: 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 number of workers SSN. The returned variable borrowercreditreport is returned for the Series retreive borrower credit report.

In instance 2, the syntax calls a sequence diagram called process credit card and passes it to the sequence number name, number, expiration date, and amount. However, in example 2, the amount is 100. Because sample 2 has no return value label, the sequence does not return values (if 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 the theirbank object. At that time, the balance lookup sequence diagram was called, and the accountnumber was passed as a number of partitions. The balance lookup Sequence Chart returns the balance variable. Then check the constraints of the selected item Combination fragment to confirm that the remaining amount is greater than the amount variable. When the remaining amount is greater than the amount, call the debit account sequence diagram and pass it the number of shards accountnumber and amount. After that sequence is complete, the withdrawcash message returns cash to the customer.

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

Sometimes, when you model a sequence chart, 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 number of records and a returned value, its label -- located in the namebox of the graph -- follows a specific pattern:

Graph type graph name [number of workers type: Number of workers]
[: 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 number of shards accountnumber as the variable in the sequence, and the sequence diagram shows the returned real object. In such a case, the returned object entity uses the sequence diagram entity name.

Figure 12: A sequence chart that uses the number of accountnumber shards and returns a real object

Figure 13 illustrates Example 2. In this example, a sequence chart obtains the number of workers and returns an object. However, in Figure 13, the sequence number is used in sequence interaction.

Figure 13: a sequence chart that uses the number of records in its interaction and returns a Reports object

Portal
The previous section shows how to transmit information through the number of arguments and return values. The reference also has a sequence diagram. However, there is another way to transmit messages between sequence diagrams. The portal may be an easy method to model a message passing between a sequence diagram and its context. A portal is just a message, and the graphic representation is the frame edge of a sequence diagram connecting one end, and one end is connected to the lifeline. Use figures 11 and 12 of the door to see the refactoring in figures 14 and 15. In the example in Figure 15, a portal called getbalance is provided to obtain the number of shards accountnumber. Because the arrow line is connected to the graph frame, and the arrow is connected to the lifeline, The getbalance message is an entry door. A sequence chart also has an exit gate and returns the balance variable. As can be seen from the exit, as it is a returned message, connecting the frame from a lifeline to the graph, and connecting the frame with arrows.

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 practical-jump and parallel.

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

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

Jump is most often 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 treats the balance <amount situation as an exception rather than a variant stream. To read Figure 16, you can 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 remaining amount than the amount. Assuming that the remaining amount is not less than the amount, the next message to be passed is the adddebittransaction message, and the sequence continues normally. However, when the remaining amount is less than the amount, the system then redirects to the combined Shard, and its message is transmitted. Once the messages in the jump combination have been transmitted, the sequence will exit without sending any other messages (for example, adddebittransaction ).

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

Parallel
Today's modern computer systems are constantly improving in complexity and sometimes running concurrent tasks. When it takes longer to complete a complex task than expected, some systems need to process all parts of the process in parallel. When you create a sequence chart to display parallel processing activities, you need to use parallel combinations 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 that is executed in parallel.

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

Figure 17 does not provide an example of the best computer system instance for objects in parallel activities, but provides an example of a series of parallel activities that are easy to understand. The sequence is as follows: hungryperson transmits the cookfood message to the oven object. When the oven object receives the message, it sends two messages (nukefood and rotatefood) to itself at the same time. 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.

Exam

    • 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, a maximum of one operating element will actually occur during execution (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. Please retake the examThe rational edgeEarly articles on activity diagrams.

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

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

UML sequence diagram

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.