Sequence Diagrams and Collaboration Diagrams Rajkumar Buyya Grid
Sequence Diagrams and Collaboration Diagrams Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software Engineering University of Melbourne, Australia http: //www. cs. mu. oz. au/~raj or http: //www. buyya. com 1
Introduction/Agenda n Pieces of UML: n Structural Diagrams n n n Behavioural Diagrams n n n Use Case Diagram Activity Diagram Sequence Diagram Collaboration Diagram State Chart Diagram Learned so far: n n Class and object diagram Component and Deployment Diagram Use case diagram, class and object diagram, class relationships Today we will focus on: n n Sequence Diagram Collaboration Diagram 2
Object Oriented Design n Design consists of the following steps : n n Refine the class diagram. Draw the interaction diagrams for the system. n Sequence Diagram n Collaboration Diagram If objects go through complex state transitions – statechart diagrams Do the above steps iteratively as needed. 3
Sequence Diagram n Shows how objects communicate with each other over time. n n n That is, sequence diagrams are used to model object interactions arranged in time sequence and to distribute use case behavior to classes. They can also be used to illustrate all the paths a particular use can ultimately produce. The sequence diagram consists of Active Objects, Messages represented as solid-line arrows, and Time represented as a vertical progression. 4
Sequence Diagram - Objects n A life line illustrates what is happening to an object in a chronological fashion. : Name Object Life line Activation 5
Sequence Diagram – Time & Messages n Messages are used to illustrate communication between different active objects of a sequence diagram. : Name 1 : Name 2 Actor Message One Message Two 6
Types of Messages n Synchronous (flow interrupt until the message has completed. n Asynchronous (don’t wait for response) n Flat – no distinction between sysn/async n Return – control flow has returned to the caller. 7
Sequence Diagram – Compilation : Compiler Linker File. System Actor Compile Load Files Compile files Save OBJ Files Link Load OBJ files Link OBJ files Write EXE file 8
Branching Flow: flow goes to different objects [if condition is met] : Editor File. System Load File : Binary. Viewer : Text. Viewer [text file] [binary file] 9
Alternative Flow: flow changes to alternative lifeline branch of the same object Editor File. System Actor Exit App [delete file] [save file] 10
Sequence diagram -example n Use case n n Add Subject Use Case to URS (University Record System): Scenario n Scenario 1 : Subject gets added successfully. n Scenario 2 : Adding the subject fails because the subject is already in the database. 11
System Design Principles n System input can take different forms. E. g. n n From a graphical user interface From a command file URS system should be designed such that the functionality can be re-used. Command reading and functionality implementation have to be separated. 12
Reading from a command file example class URS{ public static void main(String[] args){ URSDatabase u = new URSDatabase(); //Read command from file; while ( not end of file) { u. proc. Command(cmd); //Read next commad; } //Close file } } 13
Sequence Diagram – URS Add Subject Scenario u: URSDatabase proc. Cmd(cmd) parse. Command(cmd) {transient} << create >> a: Add. Sub. Cmd [if cmd. N = ADDSUB] Add. Sub. Cmd(u, cmd. A) execute() Subject(id, name) << create >> sub 1: Subject add. Subject(sub 1) 14
Creating and Deleting objects c: Client {transient} <<create>> p: ODBProxy : Transaction set. Action(a, d, 0) set. Vales(a, d, 3, 4) committed <<destroy>> 15
Collaboration Diagrams 16
Collaboration Diagrams n n n Class diagrams indicates what classes are part of our system, what they offer, how they relate, but they don’t tell us how they communicate. Collaboration diagrams show (used to model) how objects interact and their roles. They are very similar to sequence diagrams. Actually they are considered as a cross between class and sequence diagram. Sequence Diagrams are arranged according to Time. Collaboration Diagrams represent the structural organization of object. [Both sequence and collaboration diagrams are called interaction diagrams] 17
Collaboration Diagram – URS Add Subject Scenario 1: parse. Command(cmd) proc. Cmd(cmd) u: URSDatabase {transient} <<self>> 2: [if cmd. N = ADDSUB] Add. Sub. Cmd(u, cmd. A) 3: execute() {new} a: Add. Sub. Cmd <<local>> 3. 2: add. Subject(sub 1) {new} 3. 1: Subject(id, name) sub 1: Subject 18
Collaboration Diagram – URS Add Subject Scenario 1: parse. Command(cmd) <<self>> proc. Command(cmd) u: URSDatabase class URSDatabase{ private String cmd. N; private String cmd. A; private parse. Command(String cmd){ cmd. N = …. cmd. A = …. } public proc. Command(String cmd){ parse. Command(cmd); } } 19
Collaboration Diagram – URS Add Subject Scenario {transient} u: URSDatabase 2: Add. Sub. Cmd(u, cmd. A) a: Add. Sub. Cmd {new} class URSDatabase{ private String cmd. N; private String cmd. A; } public proc. Command(String cmd){ parse. Command(cmd); if (cmd. N == ADDSUB){ Add. Sub. Cmd a = new Add. Sub. Cmd(u, cmd. A); } } 20
Collaboration Diagram – URS Add Subject Scenario class abstract Command { protected String cmd; protected URSDatabase u; public abstract void execute(); } class Add. Sub. Cmd extends Command{ public Add. Sub. Cmd(URSDatabase urs, String cmd){ u = urs; // parse command set the arguments } public void execute(){ // implement here } } 21
Collaboration Diagram – URS Add Subject Scenario 3: execute() u: URSDatabase <<local>> a: Add. Sub. Cmd class URSDatabase{ private String cmd; public proc. Command(String cmd){ parse. Command(0); if (cmd == ADDSUB){ Add. Subcmd a = new Add. Sub. Cmd(……); } a. execute(); } } 22
Collaboration Diagram – URS Add Subject Scenario class Add. Sub. Cmd{ URSDatabase u; a: Add. Sub. Cmd public execute(){ subject sub 1 = new Subject(id, name); } 3. 1: Subject(id, name) } sub 1: Subject 23
Collaboration Diagram – URS Add Subject Scenario u: URSDatabase 3. 2: add. Subject(sub 1) a: Add. Sub. Cmd class Add. Sub. Cmd{ URSDatabse u; public execute(){ } } subject sub 1 = new Subject(……); u. add. Subject(sub 1); 24
Collaboration Diagram – URS Add Subject Scenario class URSDatabase{ private String cmd; private Hashtable subject. Hash = new Hash. Table(); public proc. Command(String cmd){ parse. Command(0); if (cmd == ADDSUB){ Add. Subcmd a = new Add. Sub. Cmd(……); } a. execute(); } public add. Subject(Subject sub); { subject. Hash. put(sub. get. Key(), sub); } } 25
URS -High Level Class Diagram URSDatabase 1 * 1 has * University. Member Subject 0… 10 Academic. Staff 1 Student * 0. . 3 takes teaches 26
Collaboration Diagrams n Collaborations Diagrams show transient links that exists between objects. n n <<self>> - A message from object to itself << local>> - A message sent due to the object begin defined as a local variable in the method. <<parameter>> - The object reference was sent as a parameter to the method. <<global>> The object is global. 27
Use Case Vs Scenarios n Use case n n Enroll Subject Use Case: Scenario 1 : Student is enrolled for the subject. n Scenario 2 : Enrollment fails since the student is already enrolled in 10 subjects. n 28
Sequence Diagram – Enroll Student for subject successfully stu: Student u: URSDatabase proc. Cmd(cmd) parse. Command(cmd) {transient} << create >> a: Assg. Sub. Cmd(u, cmd. A) execute() get. Student(id) return stu get. Subject(sub. Id) return sub [if stu != NULL and sub != NULL] add. Subject(sub) 29
Collaboration Diagram – Enroll Student in Subject Scenario 1: parse. Command() proc. Cmd(cmd) <<self>> 2: Add. Sub. Cmd(u, cmd. A) 3: execute() u: URSDatabase {transient} {new} <<local>> 3. 1: stu: =get. Student(id) a: Assg. Sub. Cmd 3. 2: sub: = get. Subject(sub. Id) {parameter} 3. 3: [stu !=NULL and sub!= NULL]: add. Subject(sub) stu: Student 30
Collaboration Diagram – Enroll Student in Subject subject - implementation {transient} proc. Cmd(cmd) u: URSDatabase 3: execute() {new} <<local>> 3. 1: stu: =get. Student(id) a: Assg. Sub. Cmd 3. 2: sub: = get. Subject(sub. Id) class Assg. Sub. Cmd{ private URSDatabase u; public execute(){ Student stu = u. get. Student(id); Subject sub = u. get. Subject(sub. Id); if (stu != null && sub != null){ stu. add. Subject(sub); } } {parameter} 3. 3: [stu !=NULL and sub!= NULL]: add. Subject(sub) stu: Student } 31
Sequence Diagram – Enroll Student for subject - Failure stu: Student u: URSDatabase proc. Cmd(cmd) parse. Command() {transient} << create >> a: Assg. Sub. Cmd(u, cmd) execute() [if stu != NULL and sub != NULL] get. Num. Subjects() add. Subject(sub) return num [num >= 10] Excp return e 32
- Slides: 32