UML Sequence diagram detailed (1)

Source: Internet
Author: User
Tags abstract anonymous dashed line exception handling

The main purpose of this article is to continue to focus our emphasis on the basic UML diagram; This month, we learn more about sequence diagrams. Again, please note that the example provided below is based on the new UML 2 specification.

The purpose of the diagram

Sequence diagrams are primarily used to display these interactions between objects in a series of sequences that occur interactively. Like a class diagram, developers generally think that sequence diagrams only make sense to them. However, the business people of an organization will find that the sequence diagram shows how different business objects interact and is useful for communicating how the current business is doing. In addition to documenting the organization's current events, a business-level sequence diagram can be used as a requirement file to deliver a future system delivery requirement. At the project's requirements stage, analysts can take use cases to the next level by providing a more formal level of expression. In that case, the use cases are often refined into one or more sequence diagrams.

The organization's technicians can find that sequence diagrams are very useful in documenting how the behavior of a future system should behave. In the design phase, architects and developers can use diagrams to dig out interactions between system objects, thus enriching the entire system design.

One of the main uses of sequence diagrams is to translate the requirements expressed in use cases into further, more formal levels of fine expression. Use cases are often refined into one or more sequence diagrams. In addition to the purpose of designing new systems, Sequence diagrams can also be used to record how an object that exists (called "legacy") now interacts. This document is useful when the system is handed over to another person or organization.

symbols

Since this is my first article in UML Diagram series based on UML 2, we need to first discuss a supplement to the UML 2 diagram notation, a symbolic element called a frame. In UML 2, frame elements are used as a basis for many other diagram elements, but for the first time most people touch the frame element as a graphical boundary of the graph. When you provide a graphical boundary for a diagram, a frame component provides a consistent position for the label of the diagram. The frame elements are selectable in UML diagrams, as you can see in Figures 1 and 2, the label of the graph is placed in the upper-left corner, in the "Namebox" of the frame I will call, a rectangular shape, and the actual UML diagram is defined inside a larger enclosing rectangle.

Figure 1: An empty UML 2 frame element

In addition to providing a graphical border, the frame elements used in diagrams also have important functions that describe interactions, such as sequence diagrams. On a sequence diagram, a sequence receives and sends a message (also known as an interaction) that can be modeled by connecting messages and frame element boundaries (as seen in Figure 2). This will be described in more detail in the later "beyond the basics" paragraph.

Figure 2: A sequence diagram of receiving and sending messages

Note in Figure 2, for sequence diagrams, the label of the graph starts with the text "SD". When a diagram is closed with a FRAME element, the label of the diagram needs to be in the following format:

Diagram type diagram name

A UML specification provides a specific literal value to a diagram type. (for example, the SD represents a sequence diagram, the activity represents the active diagram, and the use case represents a usage diagram).

Basis
The main purpose of a sequence diagram is to define the sequence of events that produce some desired output. The focus is not on the message itself, but on the order in which the messages are generated; however, most sequence diagrams represent what messages are passed between objects in a system and the order in which they occur. The graph passes information in the horizontal and vertical dimensions: The vertical dimension represents the time series that the message/call takes place from top to bottom, and the horizontal dimension from left to right represents the object instance to which the message is sent.

Lifeline
When drawing a sequence diagram, place a lifeline symbol element across the top of the graph. The lifeline represents a sequence, modeled role, or object instance. 1 Lifeline painting a square, a dashed line from top to bottom, through the center of the base border (Figure 3). The lifeline name is placed in the square.

Figure 3: An example of a student class for a lifeline with an entity named Freshman

The UML Lifeline naming standard is in the following format:

Entity Name: Class name

In the example shown in Figure 3, the lifeline represents the entity of the class student, and its entity name is freshman. Note here that the Lifeline name is underlined. When using underscores, it means that the lifelines in the sequence diagram represent a specific entity of a class, not a specific kind of entity (for example, a role). In a future article, we'll look at structured modeling. Now, just commenting on the sequence diagram may contain roles such as buyers and sellers, without having to describe who plays those roles (such as Bill and Fred). This allows the use of different contexts of graphs to be reused. Simple drag and drop, the instance name of the sequence diagram is underlined, and the role name is not.

The example of our lifeline in Figure 3 is a named object, but not all lifelines represent named objects. Instead, a lifeline can be used to represent an anonymous or unnamed entity. When modeling an unnamed instance on a sequence diagram, the name of the lifeline takes the same pattern as a named instance, but the location of the lifeline name leaves a blank space instead of providing an example chart name. Again, refer to Figure 3, if the lifeline is showing an anonymous example of the Student class, the lifeline would be: "Student". Also, because the sequence diagram is used in the project design phase, there is an unspecified object that is perfectly legal: for example, "freshman".

News
For readability, the first message of a sequence diagram always starts at the top and is generally on the left side of the graph. The secondary message is then added to the graph, slightly lower than the previous message.

To show an object (for example, a lifeline) passing a message to another object, you draw a line to the receiving object, including a solid arrow (if it is a synchronous call operation) or a stick-shaped arrow (if it is an asynchronous signal). The message/method name is placed above the line with the arrows. The message being passed to the receiving object that represents an action/method for the class implementation 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, including the parameter userid, and the type of the Secsystem class is Securitysystem. 2

Figure 4: An instance of passing messages between objects

In addition to just displaying the message calls on the sequence diagram, the diagram in Figure 4 also includes a return message. These return messages are selectable; a return message painting a dashed line with an open arrow pointing backwards to the source's lifeline, above which you put 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 return message is an optional part of the sequence diagram. Returns the specific/abstract extent of the use-dependent modeling of the message. The return message is useful if a good materialization is required, otherwise the active message is sufficient. I personally like that whenever a value is returned, a return message is included, because I find that extra detail makes a sequence diagram easier to read.

When a sequence diagram is modeled, sometimes an object will need to pass a message to itself. When an object is called itself. A purists would argue that an object should never pass a message to itself. However, modeling a message to its own object can be useful in some scenarios. For example, Figure 5 is an improved version of Figure 4. The version shown in Figure 5 shows the system object that called its Determineavailablereports method. By indicating that the system passes the message "determineavailablereports" to itself, the model focuses its attention on the fact of the process rather than on the system object.

To draw a call to the object itself, as you normally do, draw a message, but not connect it to another object, but you connect the message back to the object itself.

Figure 5: The System object calls its Determineavailablereports method

The message instance in Figure 5 shows the synchronization message; However, in a sequence diagram, you can also model asynchronous messages. An asynchronous message is similar to a synchronous drawing, but the line of the message is drawn with a stick-shaped spear, as shown in Figure 6.

Figure 6: Sequence picture segment representing an asynchronous message passed to entity 2

Constraints

When modeling an object's interaction, sometimes a condition must be met before the message is passed to the object. Constraints are used throughout the UML diagram to control the 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, in order to draw a constraint on a sequence diagram, you put the constraint element on the message line of the constraint, before the message name. Figure 7 shows a fragment of a sequence diagram with a constraint on the message addstudent method.

Figure 7:uml A fragment of a 1.x sequence diagram where the addstudent message has a constraint

In Figure 7, the constraint is the text "[Pastduebalance=0]". With this message constraint, if the Receivables system returns a 0-point overdue balance, the addstudent message will be passed. The symbol of the constraint is simple; the format is:

[Boolean Test]

For example,

[pastduebalance = 0]

Combined fragmentation (VARIANT scheme, selection item, and loop)
However, in most sequence diagrams, the UML 1.x "In-line" constraint is not sufficient to handle the required logic of a modeling sequence. This missing feature is a problem with UML 1.x. UML 2 has solved this problem by removing the "in-line" constraint and adding a symbolic component called the composite fragment. A combined fragment is used to combine a set of messages and display conditional branches in a sequence diagram. The UML 2 specification indicates the 11 types of interaction for combined fragmentation. Three of the 11 species will be described in the "Basics" section, and the other two types will be described in "beyond the Basics", and the remaining six will be covered in another article. (Hi, this is an article rather than a book.) I hope you will finish this part of the day. )

Variant
Variants are used to indicate mutually exclusive choices between two or more message sequences. 3 Variants Support Classic "If Then Else" logic modeling (for example, if I buy three, then I get a 20% discount on my purchase; otherwise I get 10% off my purchase).

As you will notice in Figure 8, a variant of the combined fragment element is drawn using the frame. The word "alt" is placed in the namebox of the frame. Then the larger rectangles are divided into the operating elements called UML 2. 4 operator elements are separated by dashed lines. Each operand has a constraint to test, and the constraint is placed at the upper left of the operand at the top of the lifeline. 5 if the operand's constraint equals "true", then that operand is the action element to be executed.

Figure 8: A sequence picture segment containing variant combination fragments

Figure 8 as an example of how the combined fragment of a variant is read, the display sequence starts at the top, that is, the Bank object gets the check amount and the account balance. At this point, the variant combination fragment in the sequence diagram takes over. Because the constraint "[Balance >= amount]", if the balance exceeds or equals the amount, then the Bank object is passed adddebittransaction and Storephotoofcheck messages to the account object sequentially. However, if the balance is not greater than or equal to the amount, then the order process is that the bank passes the Addinsuffientfundfee and Notereturnedcheck messages to the account object, Returncheck the message to itself. Because of the "else" constraint, the second sequence is called when the balance is not greater than or equal to the amount. The "Else" constraint is not required in the composition fragment of the variant, and if an operand does not have a definite constraint on it, then the "Else" constraint is assumed.

The combination fragment of the variant is not limited to simple "if then else" validation. A large number of variation paths may be required. If you need more variation schemes, all you have to do is add an operand to the rectangle with the sequence constraints and messages.

Select item
The selection combination fragment is used to model the sequence, which is given a specific condition that will occur, or the sequence does not occur. A selection is used to model a simple "if then" expression. (for example, if there are less than five laps on the rack, make two more donuts).

The selection combination fragment symbol is similar to the variant composition fragment except that it has only one operand and never has an "else" constraint (it is, for no reason). To draw a selection combination, you draw a frame. The text "Opt" is the text that is placed in the frame's namebox, and in the frame's content area, the selection constraint is placed on the top left corner of the lifeline. Then select the item's message sequence to be placed within the rest of the frame's content area. These components are shown in Figure 9.

Figure 9: A sequence picture segment that includes a selection combination fragment

Reading the selection combination fragment is easy. Figure 9 is the reprocessing of the sequence picture segment of Figure 7, but this time it uses a selection combination fragment, because if student's overdue balance equals 0, more messages need to be passed. According to the sequence diagram in Figure 9, if the student's overdue balance equals zero, then the Addstudent,getcostofclass and Chargeforclass messages are passed. If the student's overdue balance is not equal to zero, then the sequence does not pass any one message in the selection combination fragment.

The sequence picture segment of the example Figure 9 includes a selection constraint; however, the constraint is not a required component. In high-level, abstract sequence diagrams, you may not want to describe the criteria for selecting items. You may just want to point out that the fragment is optional.

Loops

Sometimes you will need to model a repeating sequence. In UML 2, the modeling of a repeating sequence has been improved, with the addition of cyclic combined fragments.

The looping combined fragment surface is very similar to the selection combination fragment. You draw a frame and place the text "loop" in the namebox of the frame. In the frame's content area, the top of a lifeline, the loop constraint 6 is placed in the upper-left corner. The loop message sequence is then placed in the rest of the frame's content area. In a loop, in addition to the standard Boolean tests, a constraint can test two specific conditional expressions. The specific constraint formula is the minimum number of cycles for the writing "minint = [number]" (for example, "minint = 1"), and the maximum number of cycles to write "maxint = [The]" (for example, "maxint = 5"). With a minimum loop check, the loop must run at least a specified number of times, and the number of cycles performed cannot reach the maximum number of cycles specified by the constraint.

Figure 10: A sequence diagram example of a cyclic combined fragment

The loop shown in Figure 10 runs until the hasanotherreport message of the Reportsenu object returns False. If the loop sequence should run, the loop of this sequence diagram is confirmed with a Boolean test. To read this diagram, you start at the top, as usual. When you get to the loop combination fragment, do a test to see if the value Hasanotherreport equals True. If the Hasanotherreport value equals True, then the sequence enters the loop fragment. You can then track the looping messages in the sequence diagram as normal.

Beyond the Basics

I've covered the basics of sequence diagrams and should allow you to model most of the interactions that will normally occur in your system. The following paragraphs describe the higher-order symbol components used for sequence diagrams.

Referencing a different sequence diagram
As a sequence diagram, developers love to reuse existing sequence diagrams in their sequence diagrams. 7 Starting in UML 2, the introduction of "interactive" components. The additional interaction is the most important innovation in UML 2 interaction modeling. The interaction is added to the function to organize the original sequence diagram into a complex sequence diagram. Because of these, you can combine (reuse) simpler sequences to produce more complex sequences. This means that you can abstract a complete, possibly more complex sequence into a single conceptual unit.

An interactive component is drawn using a frame. The literal "ref" is placed in the frame's namebox, and the referenced sequence diagram name is placed in the frame's content area, along with any parameters of the sequence diagram. The name symbol for the reference sequence diagram is the following pattern:

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, passed to it the parameter ssn. Sequence retreive Borrower credit report returns the variable Borrowercreditreport.

In instance 2, the syntax calls a sequence diagram called the process credit card, passed to it the parameters name,number,expiration date, and amount. However, in Example 2, the amount parameter will be the value 100. Since example 2 does not have a return value label, the sequence does not return a value (assuming that the modeled sequence does not require a return value).

Figure 11: A sequence diagram referencing two different sequence diagrams

Figure 11 shows a sequence diagram that references the sequence diagram "Balance Lookup" and "Debit account". The sequence starts at the upper-left corner and the customer passes a message to the Teller object. The Teller object passes a message to the Theirbank object. At that time, the balance lookup sequence diagram is called, and AccountNumber is passed as a parameter. Balance Lookup sequence Diagram returns the Balance variable. Then examine the constraints of the selection combination fragment to confirm that the balance is greater than the amount variable. In cases where the balance is greater than the amount, call the debit account sequence diagram and pass parameters AccountNumber and amount to it. After that sequence is complete, the Withdrawcash message returns cash for the customer.

It is important to note that in Figure 11, Theirbank's lifeline is interactively balance lookup is hidden. Because the interaction hides the lifeline, it means that the Theirbank lifeline is referenced in the Balance Lookup sequence diagram. In addition to hiding the lifeline of the interaction, UML 2 also indicates that the lifeline must have the same theirbank in its own "Balance Lookup" sequence.

Sometimes you model a sequence diagram where the interaction overlaps a lifeline that is not referenced in the interaction. In that case, the lifeline is displayed like a normal lifeline, and is not hidden by overlapping interactions.

In Figure 11, the sequence refers to the "Balance Lookup" sequence diagram. The "Balance Lookup" sequence diagram is shown in Figure 12. Because the example sequence has parameters and a return value, its label--in the namebox of the graph--follows a specific pattern:

Diagram type diagram name [parameter type: Name of parameter]
[: 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 which the Balance lookup sequence uses the parameter accountnumber as a variable in the sequence, and the sequence diagram shows the returned real object. In such a case, the returned object takes the sequence diagram entity name.

Figure 12: A sequence diagram that uses the AccountNumber parameter and returns a real object

Figure 13 illustrates example 2, in which a sequence diagram gets a parameter that returns an object. However, in Figure 13 the parameters are used in the interaction of the sequence.

Figure 13: A sequence diagram that uses parameters in its interactions and returns a Reports object

Door
The preceding paragraphs show how to pass information through parameters and return values, referencing another sequence diagram. However, there is another way to pass messages between sequence diagrams. A door can be an easy way to model the delivery of messages between a sequence diagram and its context. A door is just a message, the graph is represented as one end connecting the frame edge of the sequence diagram and the other end connected to the lifeline. Using the doors of figures 11 and 12, the refactoring can be seen in figures 14 and 15. The example diagram in Figure 15 has an entry gate called GetBalance, which gets the parameter accountnumber. Because the line of arrows is connected to the frame of the diagram, and the arrows are connected to the lifeline, the GetBalance message is an entry door. The sequence diagram also has a gate that returns the balance variable. The exit gate is the same as it is known because it is a return message, connected from a lifeline to the frame of the diagram, the arrow connects the frame.

Figure 14: Reconstruction of Figure 11, this time using the door
Figure 15: Reconstruction of Figure 12, this time using the door

Combined fragmentation (jump and parallel)
Presented in the "Basics" section earlier in this article, I covered the "Variants", "Select items", and "loops" of the combined fragments. These three combined fragments are most likely to be used by most people. However, there are two other combinations of fragments that a large number of shared people will find useful--jumps and parallels.

Jump
The jump combination fragment is almost identical in every aspect to the selection combination fragment, with the exception of two exceptions. First, the jump frame namebox the text "break" instead of "option". Second, when a jump combination fragment message runs, the closed interaction of the other messages will not be performed because the sequence breaks the closed interaction. In this way, the jump combination fragment is very much like the break keyword in C + + or Java programming languages.

Figure 16: Refactoring of sequence picture segments from Figure 8, fragments using jumps instead of variants

Jumps are most commonly used to do model exception handling. Figure 16 is the refactoring of Figure 8, but this time figure 16 uses the jump combination fragment because it treats balance < amount as an exception, not a variant stream. To read Figure 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 the balance is less than the amount. If the balance is not less than the amount, the next message that is passed is the adddebittransaction message, and the sequence continues normally. However, in the case where the balance is less than the amount, then the sequence enters the jump combination fragment and its message is passed. Once the jump combo message has been passed, the sequence exits without sending any other messages (for example, adddebittransaction).

Note One important thing about jumps is that they only cause a sequence of closed interactions to exit without having to complete the sequence described in the diagram. In this case, the jump combination is part of the Variant or loop, and then only the Variant or loop is exited.

Parallel
Today's modern computer systems continue to evolve in complexity and sometimes in the execution of concurrent tasks. When the processing time required to complete a complex task is longer than expected, some systems use parts of the parallel processing process. When creating a sequence diagram that shows parallel processing activities, you need to use a parallel combination fragment component.

Parallel combination fragments use a frame to draw, and you put the text "par" in the frame's namebox. Then you divide the frame's content segment into horizontal action elements with dashed lines. Each action element of the framework represents a thread that runs in parallel.

Figure 17:oven is an instance of an object that does two tasks in parallel

Figure 17 may not provide an example of the best computer system for an object that does parallel activity, but it provides an easy-to-understand example of a parallel activity sequence. The sequence is as follows: Hungryperson passes 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.

Summarize
A sequence diagram is a good diagram for documenting system requirements and organizing system designs. The reason that a sequence diagram works so well is because it shows the interaction logic between objects in the system in the chronological order in which they occur.

Refer to UML 2.0 superstructure Final adopted Specification (8th chapter) http://www.omg.org/cgi-bin/doc?ptc/2003-08-02 UML 2 Sequenc E Diagram Overview http://www.agilemodeling.com/artifacts/sequenceDiagram.htm UML 2 Tutorial http://www.omg.org/news/ Meetings/workshops/uml%202003%20manual/tutorial7-hogg.pdf

Footnote
1 in a complete modeling system, objects (instances of classes) will also be modeled in the class diagram of the system.

2 when reading this sequence diagram, assume that the analyst is logged into the system.

3 Note that two or more constraints that are attached to different variable gymnastics primitives may indeed be true at the same time, but only one operand at most will occur at run time (in which case the variant "wins" is not defined by the UML standard).

4 Although the operation element looks very much like the path on the road, I do not call them the path particularly. A swimlane is a UML symbol that is used on an activity diagram. Please refer to the early article on the activity diagram of the Rational Edge.

5 Typically, the lifeline with the constraint is the lifeline that owns the variable contained in the constraint expression.

6 about the selection combination fragment, the looping combined fragment does not need to place a constraint on it.

7 can reuse any type of sequence diagram (for example, a program or a business). I just found that developers prefer to break down their graphs by function.

References you can refer to the original English text 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.