Interaction between Java and UML

Source: Internet
Author: User
Interaction between Java and UML
Author: cuno studio selected from: saide.com

We will mainly discuss the UML class diagram above. Next we will discuss another UML diagram-the interaction diagram ). The interaction diagram describes the interaction process between a group of objects, or, here, we actually want to answer the question: "How does a method call process be expressed in UML ?"

UML interaction Diagram

This article describes the UML interaction diagram through a very simple transaction system. This system contains six Java classes. From the first fewArticle, We already know that the UML class diagram is to analyze JavaProgramThe effective method of structure. Figure 1 shows the class diagram of the transaction and payment system. In order to clearly describe the functions and roles of each class, figure 1 uses the color class prototype representation described earlier.

Figure 1: UML class diagram of a simple transaction system

If we trace the execution process of any Java program, we will find that this process contains one or more method calls to classes and objects. We call the object method to find the answer to a specific question or execute a specific action. In many cases, the called method also calls other methods-Methods of the same object, methods of other objects of the same class, or methods of objects of other classes. Similarly, these called methods will continue to call other methods until all the answers or actions are completed (or an exception occurs, at this time, there will be no answers or actions cannot be completed ).

A uml interaction diagram represents a method call process in graphs. It has two forms: a sequence diagram (sequence digoal) and a collaboration digoal ).

Sequence Diagram

To achieve a specific goal, you must execute a series of method calls. A typical purpose of a UML sequence diagram is to display the method call process. Figure 2 shows a sequence chart for calculating the cumulative amount in a transaction. The call starts from the calctotal () method of the sale class.CodeThe segment is given after the sequence diagram.

Glossary: UML defines an operation as a signature of a method ). The term "method" is reserved for the code that implements the operation. However, in the Java environment, the term "method" is more widely used. In a UML sequence diagram, calling an operation is called sending a message ). The sequence diagram actually describes the specific implementation of the operation, so we will use the term "method" (occasionally "message") more frequently below ).

Figure 2: Sequence Diagram for calculating the total transaction amount


             
              
/** Belongs to the sale class: * calculate the total transaction amount from the individual amount of lineitem * @ return total transaction amount */Public double calctotal () {Total = 0.0; iterator I = lineitems. iterator (); While (I. hasnext () Total + = (lineitem) I. next ()). calctotal (); Return total;}/** belongs to the lineitem class: * calculate the transaction amount of the current project * @ return transaction amount */Public double calctotal () {Total = product. calctotal (this); Return total;}/** belongs to the product class: * calculate the value of a specified number of products * @ return amount */Public double calctotal (lineitem Li) {return amount * Li. getquantity ();}
             

In order to easily grasp the overall situation of the sequence diagram, figure 1 only shows the name of the method. The detailed sequence diagram shows the parameters and return values of the method. In a sequence diagram, an object is displayed with a regular UML symbol, that is, the object uses the same shape or symbol as the class to which the object belongs (the default is a rectangle), then the object name is specified, and a colon is added, add the corresponding class name. Then, underline the name (for example, aproduct: product in figure 2 ). The Object Name (sale in example 2) can be omitted, or the class name (sender in example 2) can be omitted, but it is obviously not allowed to omit both. If the class name is omitted, the colon must be retained.

The passage of time is the vertical direction from top to bottom. Each object has a lifeline following the vertical down of the page, followed by the rectangle of the object. The method call is to draw an arrow from the lifeline of the called object to the lifeline of the called object. As long as any method of the object is in the execution state, the lifeline of the object is widened. The expanded lifeline is called the activation bar, which can be nested to indicate that another method of the same object is called during the execution of the previous method, the getquantity () method in Figure 2 demonstrates an example of activity bar nesting.

The Return Value of the method can be expressed by the dotted-line open arrow, but this is optional. In the example 2, the arrow from sale to sender is displayed.

If you want to perform iterative operations on an object set, add an asterisk before the method name (the cyclic condition is described in square brackets, optional ). In Figure 2, the sale class provides an example of iterative operations for calling lineitem objects.

Just like a UML class diagram, You need to view multipleSource codeFile to understand the information, expressed through a UML sequence diagram. Implementing reverse engineering for existing code to obtain the corresponding sequence diagram can help developers who are not familiar with the code quickly understand the workflow of the program.

Figure 3 shows the sequence diagram of the Sale complete () method, which numbered the call order (Message. The complete () method calls the other two methods of the sale class, namely calctotal () and calcpayments (). Figure 3 uses the ring callback symbol to indicate that an object is calling its own method.

Figure 3: sequence diagram of completing a transaction

If the sequence diagram is large, a screen may not be displayed. In Figure 3, by setting the option Panel of the together controlcenter modeling tool, the class name is not displayed in parallel with the object name, but below the object name, reduces the horizontal space required to display objects. If the class name is long, this display method can effectively reduce the image width, which can improve the readability of the image. However, to strictly comply with the latest UML specifications, the class name must be placed in parallel with the object name, separated by a colon in the middle, as shown in figure 2.

The complete () method calls the calctotal () method. The calctotal () sequence shown in Figure 2 is the result of the complete () sequence in figure 3. To simplify figure 3, we can omit the product object in figure 3 and its interaction with the lineitem object, so that readers can refer to figure 2 when viewing this part of content. Similar to the detail processing problem in the class diagram, whether or not to be omitted (or to which extent) depends on the user's needs. For example, readers of some sequence diagrams may want to specify various standard Java classes, such as iterators, wrappers, and collection classes. Although the sequence diagram can show the loop and branch structure to be used, it is generally better for readers to read the Java source code under the guidance of the sequence diagram.

Figure 4 uses together controlcenter to reverse engineer the complete () of the sale class and asks it to provide the sequence diagram obtained by all the detailed information. For most people, there may be too many details here. However, figure 4 also illustrates a problem, as shown in the exception object: The object created during sequence execution is located at the position it was created, rather than at the top of the sequence diagram.

Figure 4: Detailed sequence diagram generated using tools

Just like what we encountered when discussing class diagrams, the UML specification provides a large number of slightly different symbols for the sequence diagram, but the symbols described in this article are enough to get you started.

Collaboration Diagram

Another form of UML interaction diagram is collaboration diagram ). A collaboration diagram and a sequence diagram have the same semantics, but the method for arranging objects in a collaboration diagram is relatively free, which is entirely determined by the preference of the annotator. In the collaboration diagram, the order of interaction actions is determined by the message number. Some people prefer this drawing method, and many well-functional UML tools allow users to switch a diagram back and forth between a collaboration diagram symbol and a sequence diagram symbol. Some developers recommend that you use a collaboration diagram to show the interaction process between components and a sequence diagram to show the interaction process of various classes inside the component. The collaboration diagram shown in Figure 5 is equivalent to the sequence diagram shown in Figure 2. The collaboration diagram in Figure 6 is the same as that in Figure 4.

Figure 5: collaboration with second-class price

Figure 6: Cooperation with figure 4.

Conclusion: In practice, many necessary interaction sequences can be hidden in class diagrams, especially when class diagrams use class prototype and stereotype to represent specific behaviors and interaction modes. The UML interaction diagram clearly expresses the originally implicit interaction process, and also clearly illustrates the interaction process that was originally not explicitly defined in the class diagram. In other words, the UML interaction diagram is a supplement to the class diagram that tends to describe static features, so that the dynamic interaction process of objects is clarified.

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.