Chapter 10 System Sequence Diagrams Fig 10 1






























- Slides: 30

Chapter 10 System Sequence Diagrams

Fig. 10. 1

Fig. 10. 2

Fig. 10. 3

Fig. 10. 4

Fig. 10. 5

Chapter 15: UML Interaction Diagrams

15. 1 Introduction o Interaction diagrams illustrate how objects interact via messages to carry out tasks: they model the dynamic aspect of objects. o 2 types of interaction diagrams: n Sequence interaction diagrams; n Communication interaction diagrams; o We will only focus on sequence diagrams.

15. 2 Sequence and Communication Diagrams o o o Both kind of diagrams have the same role within the UML modeling tools allow the software engineer to translate one style into the other style : they are equivalent. Hence : n n n o use whatever style you like; Sequence diagrams emphasise the order of messages between objects (communication diagrams rely on numbering of the messages); Communication diagrams are more space efficient, especially if many objects are involved; Example of a sequence diagram :

o Skeleton sample code for the previous example: public class A { private B my. B = new B(); public void do. One() { my. B. do. Two(); my. B. do. Three(); } // … } o Equivalent communication diagram:

o Another Sequence Diagram : make. Payment Which can be read as follows: n The message make. Payment is sent to an instance of a Register. The sender is not identified. n The Register instance sends the make. Payment message to a Sale instance. n The Sale instance creates an instance of a Payment.

o Code for the make. Payment method of the Sale Class: public class Sale { private Payment payment; public void make. Payment( Money cash. Tendered ) { payment = new Payment( cash. Tendered ); //… } // … }

15. 3 Interaction Diagrams and Class Diagrams o Most of the effort during OOD should be spent on creating interaction diagrams not the class diagram: n n o o Interaction diagrams are much more valuable : they detail the algorithms for each method; dynamic view; A class diagram is just a summary; static view; it is very easily created after the interaction diagrams; Coding will be much easier from the interaction diagrams since they detail the many design decisions that have been made; class diagrams show the structure of the classes’ organization; Jumping from the analysis to the class diagram directly is hard; Jumping from the class diagram to coding is hard; Interaction diagrams are an intermediary step between analysis and design, and also between design and coding: they are very valuable.

15. 4 Common UML Interaction Diagram Notation Lifeline Boxes :

o Message syntax : return = message(parameter : parameter. Type) : return. Type Examples: initialize(code) Initialize d = get. Product. Description(id) d = get. Product. Description(id: Item. ID) : Product. Description o Singleton objects : n As we will see later when looking at the singleton pattern, it is very common to deal with the case when only one instance of a class will ever be instantiated (never two) in an application. n In this case the singleton pattern can be used to gain visibility to the object. n In a UML interaction pattern such an object is marked with a ‘ 1’ in the upper right corner of the lifeline box. n See Figure 15. 1 for an example.

Figure 15. 1 Singletons in Interaction Diagrams 15. 5 Basic Sequence Diagram Notation Messages :

o Many of the details of the notation are optional and are only typically shown whenever using a CASE tool to create the diagrams. Optional elements include: n The execution specification bar; n The solid bar on a found message; n The type information of messages; Using an agile approach, it is reasonable not to show these features except maybe the type information which is useful. o Message returns : n There are two ways to show the return result from a message: o Using the message syntax return Var = message(parameter). o Using a reply message line at the end of an activation bar. n For an example see Figure 15. 2

Figure 15. 2 : Two Ways to Show a Return Result from a Message o Messages to ‘self’ or ‘this’ :

o Creation of instances : n Typically we use the language independent create message:

o Object Lifelines and Object Destruction n We may show the explicit destruction of an object

o Frames in UML Sequence Diagrams : n To allow the visualization of complex algorithms, sequence diagrams support the notion of frames; n Frames are regions of the diagrams that have an operator and a guard, . Figure 15. 3:

n Typical frame operators are: Frame Operator Semantics alt Alternative fragment for mutual exclusion conditional logic expressed in the guards. loop Loop fragment while guard is true. Can also write loop(n) to indicate looping n times. opt Optional fragment that executes if guard is true. par Parallel fragments that execute in parallel. region Critical region within which only one thread can run. n Figure 15. 3 illustrated the use of a loop frame; n Figure 15. 4 illustrate the use of an opt frame:

Figure 15. 4 Using the opt Frame n Figure 15. 5 illustrate the use of the alt frame for mutually exclusive alternatives

Figure 15. 5 Using the alt Frame o Iteration Over a Collection : n A common algorithm is iterate over all members of a collection (any data structure) sending the same message to each object: See figure 15. 6 and 15. 7 for possible visual representations

Figure 15. 6 Iteration Over a Collection Using Explicit Notation Figure 15. 7 Iteration Over a Collection Using Implicit Notation

o Nesting of Frames : n Frames can be nested:

Chapter 32 More Sequence Diagrams and Contracts

Fig. 32. 1

Fig. 32. 2

Fig. 32. 3