Presentation is loading. Please wait.

Presentation is loading. Please wait.

Modeling Dynamic Behavior: Sequence and Collaboration Diagrams.

Similar presentations


Presentation on theme: "Modeling Dynamic Behavior: Sequence and Collaboration Diagrams."— Presentation transcript:

1 Modeling Dynamic Behavior: Sequence and Collaboration Diagrams

2 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour2 Interaction Diagrams Interaction diagrams model the dynamic aspects of a software system They help visualize how the system runs. They are built from use cases and a class diagram. —The objective is to show how a set of objects accomplish the required interactions with an actor (e.g., a Student in StressFree ). They show how actors and objects communicate to perform: —The steps of a use case, or —The steps of some other piece of functionality. They can show several different types of communication. —E.g. method calls, messages sent over the network —These are all referred to as messages.

3 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour3 Elements of interaction diagrams Instances of classes —Shown as boxes with the class and object identifier underlined Actors —Use the stick-person symbol as in use case diagrams Messages —Shown as arrows from actor to object, or from object to object There are two kinds of interaction diagrams: —Sequence diagrams —Collaboration diagrams (We’ll focus on sequence diagrams.) :CourseRecord addCourse

4 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour4 Sequence diagrams A sequence diagram shows how messages are exchanged among objects to complete a task (a use case) The objects are arranged horizontally across the top of the diagram. The vertical dimension represents time. An actor that initiates the interaction is shown on the left. A dotted lifeline is attached to each object or actor. The lifeline becomes an activation box during the live activation period. A message is an arrow between the activation boxes of the sender and receiver. —A message can have an argument list and a return value. :CourseOffering enrollInCourses

5 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour5 Example Sequence Diagram ***** ****** * CourseOffering requestToRegister CourseRecord Course getPrerequisites Schedule addCourse meetsPrerequisites addStudent enrollInCourses addCourse > :Schedule :CourseRecord :courseOffering addStudent

6 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour6 A Sequence Diagram showing more detail enrollInCourses (aStudent) addCourse aStudent: Student :courseRecord :courseOfferingGUI enrollInCourses :Course create prereq := getPrerequisites hasPrerequisites := hasTaken(prereq) addStudent(aStudent) [hasPrerequisites] opt

7 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour7 An iteration over objects is indicated by an asterisk preceding the message name getSubtotal :Item :Bill getPrice computeTotal :Purchase Sequence diagrams can show looping loop [0..numPurchases]

8 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour8 If an object’s life ends, an X appears at the end of the lifeline Sequence diagrams – object deletion deleteACourse deleteStudent (aStudent) cancel :CourseOffering :CourseRecord aStudent :Student deleteCourse

9 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour9 Collaboration diagrams Collaboration diagrams emphasise how objects collaborate during an interaction It is a graph with the objects as the vertices. Links are added between pairs of collaborating objects. Messages are attached to the links. —Shown as arrows labelled with the message name Time ordering is indicated by attaching a number to each message.

10 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour10 Collaboration diagrams provide an alternative to sequence diagrams 2: addCourse1: > :Schedule :CourseRecord :CourseOffering 3: addStudent enrollInCourses addCourse > :Schedule :CourseRecord :courseOffering addStudent

11 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour11 Collaboration diagrams can also show details 1: enrollInCourses(aStudent) 5: addCourse aStudent: Student :CourseRecord :CourseOfferingGUI :Course 4: [hasPrerequisite] > 2: prereq := getPrerequisites 3: hasPrerequisites := hasTaken(prereq) 5: addStudent(aStudent)

12 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour12 Communication links in collaboration diagrams A link exists between two objects whenever one object can send a message to the other. Several situations make this possible: 1.The objects’ classes have an association between them. 2.The receiving object is stored in a local variable of the sending method. -This often happens when the object is created in the sending method or when some computation returns an object. 3.A reference to the receiving object has been received as a parameter of the sending method. 4. The receiving object is stored in a local variable of the sending method. -This often happens when the object is created in the sending method or when some computation returns an object. 5.A reference to the receiving object has been received as a parameter of the sending method.

13 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour13 Choosing a sequence or a collaboration diagram? Sequence diagrams Make explicit the time ordering of the interaction (they align well with use cases) —So they are a natural choice when you build an interaction model from a use case. Make it easy to add details to messages. Collaboration diagrams Can be seen as a projection of the class diagram —Might be preferred when you are deriving an interaction diagram from a class diagram. —Are also useful for validating class diagrams. Have less space for adding details to messages.

14 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour14 Implementing Classes from Sequence Diagrams These diagrams are most useful for parts of the system that are most complex. They help you create a correct implementation. They are particularly useful when an activity is spread across several classes. The operations become the class’s methods But many operations are already indicated by the class diagram and use case preconditions and postconditions. A good system model unifies class diagrams, use cases, and sequence diagrams into a coherent whole.

15 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour15 UML Tools generate the core Java classes E.g., CourseOffering, CourseRecord, and Schedule Classes ****** * CourseOffering requestToRegister CourseRecord Schedule addCourse meetsPrerequisites addStudent /** @poseidon-generated */ public class CourseRecord { private String grade; public CourseOffering courseOffering; public Transcript transcript; public Schedule schedule; grade instance variables associations

16 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour16 … and the methods for managing associated objects public CourseOffering getCourseOffering() { return courseOffering; } public void setCourseOffering( CourseOffering courseOffering) { if (this.courseOffering != courseOffering) { if (this.courseOffering != null) this.courseOffering.removeCourseRecord(this); this.courseOffering = courseOffering; if (courseOffering != null) courseOffering.addCourseRecord(this); }

17 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour17 CourseOffering class /** @poseidon-generated */ public class CourseOffering { private int id; private String section; private String labSection; private String semYear; private int capacity; private int enrollment; public Collection courseRecord = new HashMap(); public Course course; public Instructor instructor; public TimeSlot timeSlot;

18 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour18 CourseOffering methods public boolean isFull() { } public boolean hasStudent(int id) { } public boolean addStudent(int id) { } public boolean deleteStudent(int id) { } public Collection getCourseRecords() { return courseRecord; } public void addCourseRecord(CourseRecord courseRecord) { if (! this.courseRecord.contains(courseRecord)) { this.courseRecord.add(courseRecord); courseRecord.setCourseOffering(this); } public void removeCourseRecord( CourseRecord courseRecord) { … }

19 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour19 Risks in Modelling Interactions and Behaviour Dynamic modelling is a difficult skill A large system has a very large number of possible paths. It is hard to assign the right class(es) to each behaviour. Strategies: —Work iteratively: -Develop initial class diagrams, use cases, responsibilities, interaction diagrams and state diagrams; -Then go back and verify that all of these are consistent, modifying them as necessary. —Use a good tool, like Poseidon UML, to helps ensure consistency among different diagram types. —Draw different diagrams that capture related ideas to help highlight problems. —Ensure that skilled developers lead the process. —Ensure that all aspects of the model are critically reviewed.


Download ppt "Modeling Dynamic Behavior: Sequence and Collaboration Diagrams."

Similar presentations


Ads by Google