10 Interaction Diagrams

1,549 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,549
On SlideShare
0
From Embeds
0
Number of Embeds
21
Actions
Shares
0
Downloads
87
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

10 Interaction Diagrams

  1. 1. TK2023 Object-Oriented Software Engineering CHAPTER 10 INTERACTION DIAGRAMS
  2. 2. INTRODUCTION <ul><li>UML provides interaction diagrams for illustrating how objects interact via messages. </li></ul><ul><li>Interaction diagrams are used for dynamic object modelling. </li></ul><ul><li>There are two common types: </li></ul><ul><ul><li>sequence diagram </li></ul></ul><ul><ul><li>communication diagram </li></ul></ul>
  3. 3. <ul><li>Sequence diagrams show interactions in a fence format. </li></ul><ul><ul><li>Example: </li></ul></ul>class AddressBookApp { private AddressBook addrBook; … public void addContact() { addrBook.addNewContact(); addrBook.reset(); } } … AddressBookApp app = new AddressBookApp(); … app.addContact(); : AddressBookApp : AddressBook addContact() addNewContact reset
  4. 4. <ul><li>Communication diagrams show interactions in a graph format. </li></ul><ul><ul><li>Example: </li></ul></ul>class AddressBookApp { private AddressBook addrBook; … public void addContact() { addrBook.addNewContact(); addrBook.reset(); } } … AddressBookApp app; app = new AddressBookApp(); … app.addContact(); : AddressBookApp addrBook : AddressBook addContact() 1: addNewContact 2: reset
  5. 5. SEQUENCE DIAGRAM VS COMMUNICATION DIAGRAM More difficult to see sequence of messages Fewer notation options Space economical - flexibility to add new objects in two dimensions communication Forced to extend to the right when adding new objects; consumes horizontal space Clearly shows sequence or time ordering of messages Large set of detailed notation options sequence Weaknesses Strengths Type
  6. 6. LIFELINE BOXES <ul><li>Informally, lifeline boxes represent the participants in an interaction. </li></ul>lifeline box representing an unnamed instance of class Sale : Sale
  7. 7. lifeline box representing a named instance of class Sale lifeline box representing an instance of an ArrayList class, parameterized to hold Sale objects lifeline box representing one instance of class Sale , selected from the sales collection. s1 : Sale sales : ArrayList<Sale> sales[ i ] : Sale
  8. 8. BASIC MESSAGE EXPRESSION SYNTAX Syntax for message expressions: initialize(code) initialize d = getProductDescription(id) d = getProductDescription(id:ItemID) d = getProductDescription(id:ItemID) : ProductDescription : Register : Store doA message send
  9. 9. BASIC SEQUENCE DIAGRAM NOTATION <ul><li>A (synchronous) message between objects is represented with a message expression on a filled-arrowed solid line between the vertical lifelines. The time ordering is from top to bottom. </li></ul>
  10. 10. : Register : Sale doA doB doX doC doD typical sychronous message shown with a filled - arrow line a found message whose sender will not be specified execution specification bar indicates focus of control
  11. 11. <ul><li>There are two ways to show the return result from a message: </li></ul>
  12. 12. <ul><li>A message being sent from an object to itself is shown using a nested activation bar. </li></ul>
  13. 13. <ul><li>Object creation notation is shown below: </li></ul>: Register : Sale makePayment ( cashTendered ) : Payment create ( cashTendered ) authorize note that newly created objects are placed at their creation &quot; height &quot;
  14. 14. <ul><li>The following is also permissable: </li></ul>: Register : Sale makePayment ( cashTendered ) : Payment Payment(cashTendered) authorize
  15. 15. <ul><li>Looping constructs are supported in UML via frames . Frames have an operator and a guard. </li></ul>a UML loop frame , with a boolean guard expression enterItem ( itemID , quantity ) : B endSale description , total makeNewSale [ more items ] loop : A
  16. 16. <ul><li>Conditional constructs are represented as OPT frames. </li></ul>
  17. 17. <ul><li>Mutally exclusive conditional messages are represented as ALT frames. </li></ul>
  18. 18. <ul><li>Representing iteration over a collection: </li></ul>an action box may contain arbitrary language statements ( in this case , incrementing ‘i’ ) it is placed over the lifeline to which it applies st = getSubtotal lineItems [ i ] : SalesLineItem t = getTotal [ i < lineItems . size ] loop : Sale i ++ This lifeline box represents one instance from a collection of many SalesLineItem objects . lineItems [ i ] is the expression to select one element from the collection of many SalesLineItems ; the ‘i” value refers to the same “i” in the guard in the LOOP frame
  19. 19. <ul><li>The following is simpler but excludes certain details: </li></ul>
  20. 20. <ul><li>Frames can be nested as shown below: </li></ul>
  21. 21. <ul><li>An interaction diagram can contain references to other interaction diagrams. These references are called interaction occurrences . </li></ul>
  22. 23. <ul><li>How to show polymorphism in a sequence diagram? </li></ul><ul><li>One approach is to use multiple sequence diagrams as shown in the following example. </li></ul>
  23. 24. : Register authorize doX : Payment : DebitPayment doA authorize : Foo doB : CreditPayment doX authorize : Bar Payment authorize () ... CreditPayment authorize () ... DebitPayment authorize () ...
  24. 25. BASIC COMMUNICATION DIAGRAM NOTATION <ul><li>A link is a connection path between two objects; it indicates some form of navigation and visibility between the objects is possible. </li></ul><ul><li>More formally, a link is an instance of an association. </li></ul>1 : makePayment ( cashTendered ) 2 : foo : Register : Sale
  25. 26. <ul><li>Each message between objects is represented with a message expression and a small arrow indicating the direction of the message. </li></ul><ul><li>A sequence number is added to show the sequential order of messages. </li></ul><ul><li>It is advisable not to number the starting message to simplify the overall numbering. </li></ul>1 : msg 2 2 : msg 3 3 : msg 4 : Register : Sale msg 1
  26. 27. <ul><li>A message sent from an object to itself is shown below: </li></ul>
  27. 28. <ul><li>Any message can be used to create an instance. The convention in the UML is to use a message named create . </li></ul><ul><li>If another message name is used, the message may annotated with the « create » stereotype. </li></ul>1 : create ( cashier ) : Register : Sale «create» 1 : Sale ( cashier ) : Register : Sale
  28. 29. <ul><li>The UML tagged value {new} may optionally be added to the lifeline box to highlight the creation. </li></ul>1 : create ( cashier ) : Register : Sale { new }
  29. 30. <ul><li>The order of messages is illustrated with sequence numbers. </li></ul><ul><ul><li>The first message is not numbered. </li></ul></ul><ul><ul><li>The order and nesting of subsequent messages is shown with a nested numbering scheme. </li></ul></ul>: A msg 1 : B 1 : msg 2 : C 1 . 1 : msg 3
  30. 31. : A msg 1 : B 1 : msg 2 : C 1 . 1 : msg 3 2 . 1 : msg 5 2 : msg 4 : D 2 . 2 : msg 6
  31. 32. : A msg 1 : B 1 : msg 2 : C 2 : msg 4 : D : C 1 . 1 : msg 3 2 . 1 : msg 5 2 . 2 : msg 6
  32. 33. : A : B 1 : msg 2 : C 1 . 1 : msg 3 : D 2 . 2 : msg 6 2 . 1 : msg 5 2 : msg 4 msg 1
  33. 34. : A : : B 1 : msg 2 : C 1 . 1 : msg 3 : D 2 . 2 : msg 6 2 . 1 : msg 5 2 : msg 4 msg 1
  34. 35. : A : B : C 2 . 1 : msg 5 2 : msg 4 : D 2 . 2 : msg 6 msg 1 1 : msg 2 1 . 1 : msg 3
  35. 36. : A : B : C 2 . 1 : msg 5 2 : msg 4 : D 2 . 2 : msg 6 msg 1 1 : msg 2 1 . 1 : msg 3
  36. 37. : A : B : C 2 . 1 : msg 5 2 : msg 4 : D 2 . 2 : msg 6 msg 1 1 : msg 2 1 . 1 : msg 3
  37. 38. <ul><li>A conditional message is shown by following a sequence number with a conditional clause in square brackets. The message is sent only if the clause evaluates to true. </li></ul>1 [ color = red ] : calculate : Foo : Bar message 1
  38. 39. <ul><li>The following diagram is an example with mutually exclusive conditional paths. </li></ul>msg 2 : A : B : C 1 a . 1 : msg 3 msg 1 : D 1 b . 1 : msg 5 : E 2 : msg 6 1 a [ color = red ] : 1 b [ not (color = red) ] : msg 4
  39. 40. <ul><li>Iteration notation is shown below. </li></ul><ul><li>If the details of the iteration clause are not important to the modeller, a simple * can be used. </li></ul>1 * [ i = 1 .. n ] : num = nextInt : Simulator runSimulation : Random 1 * : num = nextInt : Simulator runSimulation : Random
  40. 41. <ul><li>The following shows how to represent iteration over a collection (such as an array or a Vector ), sending the same message to each. </li></ul>1 * [ i = 1 .. n ]: st = getSubtotal : Sale t = getTotal lineItems [ i ]: SalesLineItem 1 *: st = getSubtotal : Sale t = getTotal lineItems [ i ]: SalesLineItem
  41. 42. <ul><li>As with sequence diagrams, multiple communication diagrams can be used to show each concrete polymorphic case. </li></ul>: Register authorize doX : Payment { abstract } : DebitPayment : Foo doA doB : CreditPayment : Bar doX authorize authorize

×