Tham khảo tài liệu ''bài giảng phân tích thiết kế hướng đối tượng (phần 5)'', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Use case realization Lecture Hoa Sen University Agenda Notations Dependency Interaction and Class diagrams Hoa Sen University Interaction diagrams Generalization of two more specialized UML diagram types – Sequence diagrams – Communication diagrams Illustrate how objects collaborate via messages and methods Hoa Sen University Sequence vs Communication diagram Sequence diagrams illustrate interactions in a kind of fence format : A myB : B doOne doTwo – Easier to see the call sequence – Large set of detailed notation options Communication diagrams illustrate interactions in a graph or network format doThree doOne : A 1: doTwo 2: doThree – More space-efficient myB : B Hoa Sen University Common notation: participants – lifeline box lifeline box representing an unnamed instance of class Sale :Sale lifeline box representing a named instance s1 : Sale lifeline box representing an instance of an ArrayList class, parameterized (templatized) to hold Sale objects sales: ArrayList lifeline box representing one instance of class Sale, selected from the sales ArrayList collection sales[ i ] : Sale lifeline box representing the class Font, or more precisely, that Font is an instance of class Class – an instance of a metaclass «metaclass» Font List is an interface in UML 1.x we could not use an interface here, but in UML 2, this (or an abstract class) is legal x : List related example Hoa Sen University Sequence diagram notation Lifeline boxes and lifelines Messages – Synchronous message – Found message Focus of control and execution specification bars : Register doX a found message whose sender will not be specified : Sale doA doB doC doD execution specification bar indicates focus of control Hoa Sen University typical sychronous message shown with a filledarrow line Sequence diagram notation Illustrating reply or returns : Register – Using the message syntax returnVar = message(parameter) doX – Using a reply message line : Sale d1 = getDate Message to “self” or “this” getDate public class Register{ public void doX(){ … clear(); … } public void clear(){ } } aDate : Register doX clear Hoa Sen University Sequence diagram notation : Register note that newly created objects are placed at their creation "height" : Sale Creation of instance – In implementation: invoke the new operator and call the constructor Object lifelines and object destruction – In language that does not have garbage collection makePayment(cashTendered) create(cashTendered) : Payment authorize : Sale create(cashTendered) : Payment «destroy» Hoa Sen University X the «destroy» stereotyped message, with the large X and short lifeline indicates explicit object destruction Sequence diagram notation Diagram frames in UML sequence diagrams – Support conditional and looping construct : A : B makeNewSale a UML loop frame, with a boolean guard expression loop [ more items ] enterItem(itemID, quantity) description, total endSale Hoa Sen University Sequence diagram notation Common frame operators Frame operator Meaning Alt Alternative fragment for mutual exclusion conditional logic expressed in the guards Loop Loop fragment while guard is true 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 Hoa Sen University 10 Sequence diagram notation Messages to classes to invoke static methods public class Foo{ public void doX(){ Locale[] locales = Calendar.getAvailableLocales(); } } message to class, or a static method call doX : Foo 1: locs = getAvailableLocales Hoa Sen University «metaclass» Calendar 18 Sequence diagram notation Payment {abstract} Payment is an abstract superclass, with concrete subclasses that implement the polymorphic authorize operation Polymorphic messages and cases authorize() {abstract} CreditPayment DebitPayment authorize() authorize() object in role of abstract superclass polymorphic message :Register :Payment {abstract} doX authorize :DebitPayment stop at this point – don’t show any further details for this message :Foo authorize :CreditPayment :Bar authorize doA doX doB separate diagrams for each polymorphic concrete case Hoa Sen University 19 Sequence diagram notation Asynchronous and synchronous calls – Call does not wait for a response – Asynchronous calls are used in multi-thread application – Show as a stick arrow message – It is common for modeler to use stick arrow to represent synchronous message – Do not assume the shape of the arrow is correct Hoa Sen University 20 Sequence diagram notation Hoa Sen University 21 Collaboration diagram notation Links 1: makePayment(cashTendered) 2: foo : Register :Sale 2.1: bar – A connection path between two objects – Messages flow along link link line Messages – Message between objects is represented with a message expression and small arrow indicating the direction of the message – A sequence number is added to show the sequential order of messages in the current thread of control – Many messages may flow along a same single link msg1 : Register 1: msg2 2: msg3 3: msg4 :Sale 3.1: msg5 all messages flow on the same link Hoa Sen University 22 Collaboration diagram notation (cont) msg1 Message to “self” or “this” Creation of instances : Register 1: clear – Use a message named create for this purpose Create message may have parameters – Indicates a constructor call with parameters in Java Hoa Sen University 23 Collaboration diagram notation (cont) Message number sequencing – OK to use flat numbering style or – Nested decimal numbers Clarify whether a message is called within another message msg1 : A 1: msg2 : B 1.1: msg3 not numbered legal numbering Hoa Sen University : C 24 Collaboration diagram notation (cont) first second third msg1 1: msg2 : A : B 1.1: msg3 2.1: msg5 2: msg4 fourth : C fifth 2.2: msg6 sixth Hoa Sen University : D 25 Collaboration diagram notation (cont) Conditional messages – A conditional message is shown by following a sequence number with a conditional clause in square brackets The message is only sent if the clause evaluates to true – Attach a letter to the sequence number to express mutually exclusive message conditional message, with test message1 1 [ color = red ] : calculate : Foo unconditional after either msg2 or msg4 : E : Bar 1a and 1b are mutually exclusive conditional paths 2: msg6 msg1 1a [test1] : msg2 : A : B 1b [not test1] : msg4 : D Hoa Sen University 1b.1: msg5 1a.1: msg3 : C 26 Collaboration diagram notation (cont) Iteration or Looping – Use a “*” to signify iteration runSimulation : Simulator 1 * [ i = 1 n ]: num = nextInt : Random iteration is indicated with a * and an optional iteration clause following the sequence number Hoa Sen University 27 Collaboration diagram notation (cont) Iteration over a collection t = getTotal : Sale 1 * [i = 1 n]: st = getSubtotal this iteration and recurrence clause indicates we are looping across each element of the lineItems collection t = getTotal : Sale lineItems[i]: SalesLineItem 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 comes from the message clause 1 *: st = getSubtotal lineItems[i]: SalesLineItem Less precise, but usually good enough to imply iteration across the collection members Hoa Sen University 28 Collaboration diagram notation (cont) Messages to a Class Object message to class, or a static method call doX : Foo 1: locs = getAvailableLocales Hoa Sen University «metaclass» Calendar 29 Collaboration diagram notation (cont) Polymorphic messages and cases polymorphic message doX :Register stop at this point – don’t show any further details for this message authorize :Payment {abstract} authorize :DebitPayment object in role of abstract superclass authorize doA doB :Foo :CreditPayment doX :Bar separate diagrams for each polymorphic concrete case Hoa Sen University 30 Collaboration diagram notation (cont) Asynchronous and Synchronous calls startClock :ClockStarter 1: create 3: runFinalization System : Class asynchronous message 2: run :Clock active object Hoa Sen University 31 Interaction and Class Diagrams : Register : Sale makePayment(cashTendered) makePayment(cashTendered) messages in interaction diagrams indicate operations in the class diagrams Register makePayment(…) Sale currentSale Hoa Sen University classes identified in the interaction diagrams are declared in the class diagrams makePayment(…) 32