Java Modeling: UML workbook: Part 1

Source: Internet
Author: User


Java Modeling: UML workbook: Part 1 Original ENGLISH


Content:
Two types of sequence diagram
Conditional Logic
DrawForCycle chart
DrawWhileCycle chart
Conclusion
References
About the author
Comments on this article


Related content:
Create a UML sequence diagram with a style
Object-Oriented Design Process: Case Introduction
All articles in the Java modeling Series


Conditional logic in sequence diagram

Granville Miller (rmiller@togethersoft.com)
Consultant, togethersoft
June 2001

Granville continues to discuss how to create a unified modeling language and sequence diagram. He carefully studied the role of conditional logic in the process of drawing sequence diagrams and discussed why conditions and loops should be included or excluded in the diagram. Granville also describes two types of Sequence Diagrams-general and instance-and their respective applications during the development cycle.

I have explained in the introductory column that a sequence diagram is used to describe the internal behavior of the system over time. Because the system behavior is the result of sending messages between objects, the sequence diagram shows the routes for moving messages between objects. In the final analysis, a sequence diagram is an interaction diagram. In the previous section, although we described countless interactions, we only created a simple graph. This time, we will conduct further research to see the two types of sequence diagrams specified by UML. These two forms are:GeneralAndInstance. Let's start with the correct application of each form.

Two types of sequence diagram
A sequence diagram describes two different types of interaction between objects. One interaction type isRequired (must)Interaction. Object A must send a specific message to object B. Another interaction type isPossible (May)Interaction, where object a May (but not necessarily) Send a specific message to object B. These two forms of sequence diagram describe the two different types of interaction. The conventional form describesRequiredInteraction, while the instance form describesPossibleInteraction.

Regular Sequence diagrams describe class interactions produced by initial stimulus factors. The conventional form describes all interactions that can be produced by the initial stimulus. The conditions for success and failure are the same as those for loops, conditions, and branches.

The regular sequence diagram contains only one class name in each box on the horizontal axis, as shown in 1. It means that the objects behind the interaction are anonymous, and any objects in the class can participate in the interaction. Therefore, it is necessary to clearly model all paths. In a regular sequence diagram, object a must send a message in the model to object B.

Figure 1. Regular Sequence Diagram

The second form of the sequence diagram is the instance form. The instance sequence diagram describes a single message exchange between two instances. Such a chart contains a variable name and its class type in the horizontal axis, as shown in figure 2. This form does not include the Common loops, conditions, and branches in the conventional form. In the actual control process of the system, certain assertions made during the interaction may be false. If the assertion is false and all messages in the instance sequence diagram are empty, this will not happen. The instance sequence diagram describes a single situation that may or may not occur.

Figure 2. instance sequence diagram

The instance sequence diagram is most suitable for modeling individual solutions during the analysis phase of the software development lifecycle. A general sequence diagram can be used to model the entire use case that contains multiple solutions. Other types of activities-for example, modeling of protocols used between subsystems or frameworks and their respective parts-can take any form, depending on the position of components in the software development lifecycle. Compared with the instance form, the conventional form is closer to the actual appearance in the final product.Code.

We used the regular form in the previous column and will continue to study it here. This time, we will explore the role that conditional logic plays in regular sequence diagrams, allowing you to learn more about UML representations.

Conditional logic in sequence drawing
Regular Sequence Diagrams use the conditional logic, which is useful for describing the optional process of events in the interaction process. Based on the different stages in the software development lifecycle, you can draw different details. In the analysis phase, you may be willing to exclude the details from the conditional expressions, while in the design phase, however, you may want to include the snippets of the Code to be used in the final product in the conditional expression.

Regardless of the stage in the development cycle, with the drawing of conditional expression graphs, the natural consistency between sequence diagrams and object-oriented languages such as Java becomes increasingly clear. For example, consider a banking application that allows the cashier to accept deposits. In addition to other items, It is also stipulated that the system must prevent the cashier from deblocking the negative amount into the account, because this will lead to deduction from the account. Therefore, the system must have a mechanism to check that all amounts entered are positive. Listing 1 shows a conditional expression that ensures that deposits are positive.

Listing 1. Methods with conditional expressions

\ ** This is a method in a teller class ** \ public void receivedeposit (account Account account, bigdecimal deposit) throws improperdepositexception {// check to ensure the deposit is positive if (deposit. compareto (New bigdecimal (0.0)> 0) {account. credit (deposit);} else {Throw new improperdepositexception ();}}

In the analysis phase, you are not very concerned about the details, so the figure only needs to indicate that the deposit is a positive number. In a regular sequence diagram, a condition appears as a protection mechanism with a message name, above the horizontal call arrow. These protection conditions are enclosed in square brackets and placed on the left side of the message name, as shown in 3.

Figure 3. conditions added during analysis

The relationship between the above method and the sequence diagram is clearer in Figure 4. in figure 4, we see a clearer diagram that may be used in the design phase. Of course, not all methods are displayed: The else clause is missing. However, the semantics of the message arrow in the figure specifies that messages can only be sent when the condition is valid.

Figure 4. Clearer Conditions

You canTellerClass andImproperdepositexceptionAdd another call Arrow between them to model the else clause. In this call, there will be a condition opposite to If; in this example, the deposit must be less than or equal to 0. You may try to model this statement yourself.

DrawForCycle chart
As shown in the preceding example, the general sequence diagram-and in fact all UML diagrams-almost map the syntax of the Java language. Therefore, most Java developers have an intuitive understanding of these graphs and can quickly learn how to use them. To further explore the consistency between regular sequence diagrams and Java languages, we will plotForA cycle chart is shown in Listing 2.

List 2.ForLoop

For (INT I = 0; I <4; I ++) {squaroom. examinecorner (I );}

In a sequence chart, iterations are represented by an asterisk (*) before the message name on the horizontal arrow. This is rare if the number of iterations is known and fixed-this number appears in square brackets behind the asterisk. Because mostForThe complex logic of loop processing does not allow you to determine the number of iterations statically, so you do not often use parentheses in this format. Figure 5 shows the precedingForCyclic sequence diagram.

Figure 5. For loop sequence diagram

DrawWhileCycle chart
BecauseWhileA loop combines loops with conditions, so it is very easy to accept. We willWhileCycle Graph.

Listing 3. While Loop

While (value. notfound () {value = database. Search (key );}

OurWhileA cycle chart contains both conditions and asterisks indicating iterations, but you will find that there are no iterations.WhileA loop rarely contains iterations-unless it is a disguisedForLoop. Figure 6 showsWhileCycle chart.

Figure 6.WhileCyclic sequence diagram

Conclusion
Generally,RequiredAndPossibleBehavior is the basic concept of UML and software development. Case captureRequiredBehavior; Scheme capturePossibleAction. Class chart capturingRequiredBehavior; instance image capturingPossibleAction. I mainly discuss this concept because I found that many people failed to grasp the fundamental flexibility of sequence diagrams, and divided them into intuition and form to use these two extremes.

Read theseArticleYou should focus on developing the intuitive understanding of model semantics. As you see more and more sequence diagrams and start to create your own sequence diagrams, you will find that many sequence diagrams depend on the condition logic and the chart context to show what the diagram representsRequiredOrPossibleSystem View. As we go deep into more complex chart rendering technologies, learning how to identify and use these differences early will help you in the future.

Besides exploring how to draw sequence diagramsRequiredAndPossibleIn addition to the importance of behavior, I also show you how to represent conditions and iterations in the graph. Now that you know how to drawForAndWhileCycle Graph. We recommend that you construct it in other Java (for exampleDo-whileLoop. As you practice drawing these simple structural diagrams, you will naturally learn more about sequence diagrams.

References

  • By clickingDiscussionTo participate in the Forum on this article.

  • For an introduction to "Unified Modeling Language" and sequence diagram drawing, see the first column in this series.
  • For more information about UML and sequence diagram drawing, see Hans-Erik Eriksson and Magnus penker'sUML Toolkit(John Wiley & Sons, 1997 ).
  • Another very helpful resource isUML in a nutshellBy Sinan Si alhir (O 'Reilly & Associates, 1998 ).
  • The authoritative source of the "UML standard" is the Unified Modeling Language Specification (uniied Modeling Language Specification) of OMG (version 1.4 was used in this article ).
  • For more information about sequence diagrams and UML, see "Creating a UML sequence diagram with styles" by Scott W. Ambler (developerworks, February 2001 ).
  • Allen Holub provides an in-depth description of the use case solution in his series of object-oriented design processes (developerworks, January 2001 ).
  • Read OCL, which is the expression language of UML.
  • IBM and other industry leaders have created XMI, a new open industry standard, it combines the advantages of some XML standards based on Web for definition, validation, and sharing of document formats with the advantages of UML.


About the author
Granville has been in the object-oriented community for 13 years. He is a co-author of the Advanced use case modeling series and has introduced tutorials at various object-oriented technical conferences around the world. His practical method of object-oriented development comes from his experience working in a number of companies, including various companies from startups to well-developed software giants.

He is currently teaching workshops, tutorials, and courses on flexible processes, methods, and Java technologies, as well as coaching and helping implement some active projects. Granville can be contacted through a rmiller@togethersoft.com.

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.