Java and UML interaction

xiaoxiao2021-03-06  77

UML software engineering organization

Java and UML interaction

Author: This article taken from Cactus Studio: SEOUL

As we focus on, we are the UML class diagram. Here we want to discuss another UML diagram-interacting Diagram. The interactive diagram describes the interaction process between a set of objects, or says that we actually answer such a question: "How is the method calling process not in UML?"

UML interaction

This article will illustrate the UML interaction through a very simple trading system. This system contains six Java classes. From the previous article, we already know that the UML class diagram is an effective way to analyze the Java program structure, and Figure 1 shows the class diagram of this transaction and payment system. In order to more clearly illustrate the functions and roles of each class, Figure I uses the color-type prototype representation previously described.

Figure 1: UML class diagram of a simple trading system

If we track any of the Java programs, this process will find that this process contains one or more ways to call the class and objects. We seek a specific problem with answers or perform a specific action by calling the object's method. Many times, the way to be called also calls other methods - or the method of the same object, or other objects of the same class, or methods of other classes. Similarly, these calling methods will continue to call other methods until the problem has been made with a clear answer or all the execution of the action (or abnormally, the problem will not have an answer or action.). The UML interaction map represents a graphical formation process, which has two forms: Sequence Diagram and Collaboration Diagram.

Sequence Diagram

To achieve a particular goal, you must have a range of methods to call. Typical uses of the UML sequence diagram are to show the method calling process. Figure 2 shows a sequence diagram of calculating the accumulated amount in a transaction business, and calls from the CalE class's CalcTotal () method, the associated code snippet is given after the sequence diagram. Terminology Description: UML defines an operation as a method of the method (SIGNATURE). Method This term is preserved to the code that implements the operation. However, in the Java environment, the term "method" is broader. In the UML sequence diagram, call an operation is called a message (Message). The sequence diagram actually elaborates the specific implementation of the operation, so we will use more "method" terms more (occasionally "message".

Figure 2: Sequence diagram of calculating total amount of transactions

/ ** belongs to the Sale class:

* Calculate the total amount of transactions from the single amount of LineItem

* @return total transaction

* /

Public Double Calctotal () {

Total = 0.0;

Iterator I = lineItems.Item ();

While (I.hasNext ()) Total = ((lineItem) i.next ()). CALCTOL ();

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:

* How much is calculated to calculate the specified number of products?

* @Return

* /

Public Double Calctotal (LineItem Li) {Return Amount * li.getquantity ();

}

In order to facilitate the overall situation of the sequence diagram, the name of the method is shown. The detailed sequence diagram shows the parameters and return values ​​of the method. In the sequence diagram, the object is displayed in a conventional UML symbol, which is the same shape or symbol like the class belonging to the object (default is a rectangle), and then indicates the name of the object, plus a colon, plus the corresponding class name. . Then add the next underscore for the entire name (for example, Aproduct: Product: product in Figure II). You can omit the name of the object (for example,: Sale in Figure II), or you can omit the name of the class (e.g., the seneder of Figure II), but both will omitly not allowed. If the name of the class is omitted, the colon must be retained. The passage direction of time is from top to bottom vertical direction. Each object has a lifeline that is vertically downward, followed by the rectangle of the object. The method called by the method is that drawing an arrow that is pointed to the lifetime of the calling object. As long as any method of the object is in the execution state, the lifeline of the object is widened. After the widening, the live line is called "Activation Bar), and the active bar can be nested, indicating another method of the same object during the execution of the previous method, and the GetQuantity () method of Figure II An example of a moving bar nested. The return value of the method can be represented by the form of a broken line fork arrow, but this is optional, such as the arrow pointing from: Sale point to Sender. If it is an iterative operation on a collection of an object, add an asterisk in front of the name of the method (in square brackets, optional). In Figure II, the Sale class gives an example of an iteration operation to the call of the LineItem class object. Like the UML class diagram, you originally needed to see more information files to understand, you can expressed through a UML sequence diagram. The corresponding sequence diagram of the existing code is implemented, which can help develop developers who are not familiar with the code quickly understand the workflow of the program. Figure 3 shows a sequence diagram of the Sale class completion () method, which number is numbered in the call order (message). The Complete () method calls two other ways of the Sale class, named CalcTotal () and CalcPayments (). Figure three-ring callback symbol indicates an object that is calling itself.

Figure 3: Completing a sequence diagram of a transaction

If the sequence diagram is large, there may be a situation where a screen cannot be displayed. In Figure 3, the name of the class is no longer displayed in parallel by setting the Modeling Tool Together ControlCenter, but is displayed below the object name, but the horizontal space required to display the object is reduced. If the name of the class is very long, the graphic width can be effectively reduced in this display method, typically improve the readability of the graphic. However, if you want to strictly comply with the latest UML specification, the name of the class must be placed in parallel with the object name, and the inner colon is separated, as shown in Figure 2. The COMPLETE () method calls the CalcTotal () method, and the CalcTotal () sequence shown in Figure II is the result of the graph three Complete () sequence. If you want to simplify the figure, we can omit the three PRODUCT objects and how it interacts with the LineItem object, allowing the reader to see Figure 2 when viewing this section. Like the details of the details facing in the class diagram, is it necessary to omit (or, which extent, which extent), must also be determined according to the needs of the user. For example, the readers of some sequence diagrams may wish to indicate various standard Java classes, such as iterators, packages, and collections, etc. Although the sequence diagram can display the cyclic and branch structure to be used, in general, the details of this level are best to get the reader by reading Java source code under the guidance of the sequence diagram. Fig. 4 is a reverse engineering using TOGETHER CONTROLCENTER to implement reverse projects for the Sale class, and requires it to give all the sequence diagrams obtained by detail. For most people, the details of the details may be too much. However, Figure 4 also illustrates a problem, as displayed by the Exception object: The object created during the sequence execution is drawn in the position it created instead of the top of the sequence diagram. Figure 4: Detailed sequence diagram generated using tools

Just like we have encountered when discussing the class diagrams, the UML specification also provides a large number of small difference symbols, but the symbols described herein are enough to get you.

Collaboration map

Another form of UML interaction is a collaboration diagram. Collaborative diagrams and sequence diagrams are the same in semantics, but the way of collaborative diagram is arranged freely, and it is completely determined by the preferences of the plotter. In the cooperative diagram, the order of interactive action is determined by the number of the message. Some people prefer this drawing method, and many of the most perfect UML tools allow users to convert one diagram between collaborative diagram symbols and sequence diagram symbols. Some developers suggested that the interaction process between the components is displayed with a collaborative diagram, and the interaction process of each class inside the component is displayed with a sequence diagram. The collaboration map shown in Figure 5 is equivalent to the sequence diagram shown in Figure II, and the cooperation map of Figure 6 is the same as the sequence diagram of FIG.

Figure 5: Collaboration with Figure II

Figure 6: Collaboration with Figure 4 equivalent

Conclusion: In practice, many required interactions can be hidden in class diagrams, especially when class graphs are used, and STEREOTYPE indicates a specific behavior and interactive mode. The UML interactive map explicitly expresses the original implicit interaction process, and it also explicitly illustrates the interaction process that is not clear in the class diagram. In other words, the UML interaction is a supplement to the class diagram that tends to describe the static feature, making the dynamic interaction process of the object clearly.

Copyright: UML Software Engineering

转载请注明原文地址:https://www.9cbs.com/read-92841.html

New Post(0)