It is now February, And now you may have read or heard people talk about the changes made in UML 2.0, including the new specifications of several progressive UML. Considering the importance of the new specification, we are also modifying the basis of this article series to focus our attention on the omg uml 1.4 specification, the transfer to OMG has adopted the draft UML 2.0 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, but the standardization of the UML 2.0 draft is an important step forward, and I feel that I need to expand the text. For some reason, OMG improved UML. The main reason is that they want UML models to express the model-driven architecture (MDA), which means that UML must support more model-driven symbols. At the same time, the UML 1.x symbol set is sometimes difficult to apply to large applications. In addition, symbol elements need to be improved to make the image easier to read. (For example, the logical process of the model in UML 1.x 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: "The uml2.0 draft Specification has been adopted ." Indeed, the specifications are still in the draft State, but the key is that the draft specifications have been adopted by OMG, which is an organization that uses them until the new standards are quite reliable. Before UML 2 is fully adopted, there will be some modifications to the specification, but these changes should be minimal. The major change will be within UML-including the features used by software companies that typically 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 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 personnel of an organization will find that 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 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 sequence diagrams. 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 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 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 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 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, 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:
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 image 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: 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:
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 an underline is 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, which 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. In Figure 3, the lifeline example 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. When a sequence chart is modeled for 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 chart name. Refer to Figure 3 again. 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 (for example, a lifeline) and send a message to another object, you draw a line pointing to the receiving object, including a solid arrow (for 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. 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, 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, 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 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 itself. 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 shows the system object that calls its determineavailablereports method. The system transmits the message "determineavailablereports" to itself. The model focuses on the fact of the process rather than the system object. 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 the constraints of UML 1.x and UML 2.0. In UML 1.x, a constraint can only be allocated 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]". 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:
For example,
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 solved this problem 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 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 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 shows how the sequence starts 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 the constraint "[balance> = amount]", if the balance exceeds or is equal to the amount, the Bank object will be delivered with the adddebittransaction and storephotoofcheck messages to the account object in sequence. However, if the balance does not exceed or equal to the amount, the sequential process is that the bank sends the addinsuffientfundfee and notereturnedcheck messages to the account object, and the 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. The "else" constraint is not required in the combination fragment of variants. If an operator element does not have a definite constraint on it, the "else" constraint is 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 ). 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 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 the re-processing of the sequence diagram fragment in Figure 7. However, this time it uses a combination fragment of a selection item, because if student's overdue balance is equal to 0, more messages need to be transmitted. According to the sequence diagram in Figure 9, if the balance of student's overdue period is equal to zero, 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, the sequence does not pass any message. 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, 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 standard Boolean tests, one constraint can test two specific conditional statements. 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 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 (click to enlarge) The cyclic Operation shown in Figure 10 is performed until the hasanotherreport message of the reportsenu object 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 combined parts of the loop, perform a test to check whether the value of hasanotherreport is true. If 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. |