Unit 7 - States and Activities Flashcards
(a) Why must the conditions on a message send be mutually exclusive in a sequential system?
(b) Figure 2 shows two conditional message sends originating at the same moment on the timeline, meaning that all the guards are evaluated before any message is sent. Could the same behaviour be implemented if the messages (and their conditions) were separated on the time line?
(c) Suggest a reason why an object’s lifeline must branch in cases such as Figure 2.
(d) Are the examples of sequence diagrams in Unit 6 generic or instance interaction diagrams?
(e) How would you change the sequence diagram in Figure 3 to sound the car’s horn five times each time it moved forward?
(a) If conditions were not mutually exclusive, more than one condition might be true. This would mean that multiple messages would be sent at once, leading to multiple receiving objects being active at the same time – violating the assumption that the system is sequential and not concurrent.
(b) Yes, provided that the conditions were mutually exclusive, so that the first message could not affect the state of the receiver before the second message arrived. We might use code such as the following.
if (room.ready)room.accept(jill);
if (!room.ready)room.requestCleaner();
(c) The Room object will receive different messages depending on which branch is executed in the Hotel object. It makes no sense for the messages to arrive at just one lifeline, as that would imply that the Room object received all of the messages in a single interaction. Instead, we show a fork in the lifeline of the receiving object to reflect the fork of control in the sending object.
(d) All the examples in the course text in Unit 6, except Figures 12, 17 and 18, are instance interaction diagrams, as they show a specific (concrete) sequence of messages. (Figures 1 and 2 of this unit are not pure generic interaction diagrams either, as they still contain concrete instances of certain classes.)
(e) To get the horn to sound five times for every forward movement in the sequence diagram, you would add an iteration clause to the second, nested message: *[j := 1..5] sound().
How does UML distinguish a synchronous message send from an asynchronous message send? How is a return from a synchronous message send indicated?
A synchronous message send has a solid arrowhead and a continuous shaft, whereas an asynchronous message send has a stick arrowhead and a continuous shaft. The return from a synchronous message send is marked by a stick arrowhead with a dashed shaft.
What properties of a message or a signal are used to express a timing constraint?
The sendTime and receiveTime properties of a message or a signal are used to express a timing constraint. In addition, a message label is required to express the time difference between the start and end points, as shown in the examples in Figure 8.
(a) What is the main disadvantage of defensive programming?
(b) Which of the three strategies is being used when we place methods such as createGuest(lastname: String, firstname: String, address : String) in the Hotel class?
(c) Are use case objects consistent with the twin aims of high cohesion and low coupling (terms that were introduced in Unit 1)?
(a) The main disadvantage of defensive programming is that the constraints within each precondition end up being repeatedly checked in both the client and the supplier.
(b) We are following the strategy of using one central class here.
(c) Yes and no. Yes, because high cohesion asks us to ‘do one thing and do it well’. A CheckerIn class should only support the one activity of checking in. No, because having an extra class for each use case does introduce more coupling through the extra associations involved. However, the new classes introduce barriers that limit the impact of a change to a software system.
Suppose you were designing the graphical user interface (GUI) part of a hotel system, and you were concerned with checking in people known to the system, as well as new people unknown to the system. What operations on the business model might be needed to support a usable GUI?
Checking in unknown people would involve the creation of a new Guest object. So somewhere in the model, there must be an operation createGuest(firstName, lastName, address).
To check in a known person, we will need to find the business object that corresponds to that person. This mapping might be done by having a pick list of all people known to the system, which would require a business-model operation that returns a collection of guests, for example, getAllGuests.
Recall the library system, which you first saw in Unit 3. Suppose you wanted to minimise the dependency of the user interface on the rules and concepts of the library domain. Which messages should be sent from the user interface, and to which object should the user interface send its messages?
A solution can be found from the first of the three strategies that were discussed above. The messages sent from the user interface correspond to each of the library’s use cases. Look at Figure 10 of Unit 3, from which you can identify 6 messages originating from the user interface:
- reserveBook(…);
- issueCopyOfBook(…);
- returnCopyOfBook(…);
- updateCatalogue(…);
- enrolNewMember(…);
- lookThroughCatalogue(…) or, more simply, browse(…).
Each one would be sent to an instance of one central class, typically a general object like a System or a Library. Although borrowing books and returning books might share common operations, all 6 messages might be needed to maintain the traceability between the requirements and your implementation.
(a) Why might two objects of the same class respond differently to the same message?
(b) What is the most common form of event that causes a transition between two states? How is it shown in a statechart diagram?
(c) What is the difference between an event and an action?
(d) What is the main constraint on the kinds of action that may be shown in a state diagram?
(e) What is an action sequence?
(a) An object’s behaviour will in general be affected by the values of its attributes, which are part of the object’s state. If two objects have different values for the same attributes, they are in different states, and therefore they might respond differently to the same message.
(b) The receipt of a message is the most common form of event that causes a transition between states. An event is used to label the transitions between the states. In a statechart diagram, a transition is labelled with the name of the relevant message, which includes any arguments for that message.
(c) An event is something done to the object, such as sending it a message. An action is something that the object does, such as sending a message to itself or to another object. An action is an object’s reaction to an event.
(d) Actions should only refer to things that the object ‘knows’ about. For example, they can refer to attributes, operations and links of the object, as well as to the parameters of the message that caused the transition. An action cannot refer to the state or attributes of another object unless there is some way for these other attributes to be known in a short time without requiring any state changes.
(e) An action sequence is an ordered series of individual actions that are associated with a particular event. They are written as a list separated by semicolons, and are performed sequentially in left-to-right order. Like actions, action sequences are atomic.
(a) What is an entry event, and how does it contribute to the maintenance of a state diagram?
(b) What is the benefit of using an internal event as opposed to a self-transition?
(a) An entry event can be used where there are multiple transitions, with the same actions, leading to a particular state in a given diagram. An entry event occurs every time an object enters the state that it annotates. Entry events reduce the risk of introducing errors, because the action sequence is written once (associated with the entry event of the state) rather than many times (on each of the transitions leading to that state).
(b) When there are entry and exit events that might interfere with a self-transition, an internal event is useful, as the entry and exit events are not triggered.
(a) What does a final state signify in a state machine?
(b) In what ways does a final state differ from an initial state?
(a) A final state is used to show the point or points where the object in question has finished processing or has reached the end of its life and will be destroyed. Although we might show more than one final state in a statechart diagram, each copy represents the same final state. (In the next subsection, you will see that a stop marker can be used to indicate that a composite state has finished executing.)
(b) There can be zero, one or more final states but at most one initial state. A final state can be active for a period of time, whereas an initial state results in an immediate transition to a successor state. A final state can have several incoming transitions and no outgoing transitions, but an initial state has no incoming transitions and only one outgoing transition.
(a) Identify three problems associated with complex state diagrams that might arise when designing classes.
(b) Suppose the class Copy included the attributes returnDate, libraryNumber and classification. Which of those attributes are significant for a state machine for the class Copy that has two states called on shelf and on loan? Explain your choice.
(c) Suppose you implemented your design for a class without using a state diagram. Are there any subsequent activities where a state machine might help you as a developer? Briefly explain your answer.
(a) Classes with complex state machines can cause three kinds of problem. First, it is harder to write the eventual code for such classes, since there are likely to be many conditional tests to identify the actual state. Second, it is harder to test the class because of the number of choices of pathway through the conditional tests. Third, it is much harder for external code to use a class correctly without some means of ascertaining the actual state of objects belonging to complex classes.
(b) State machines help model what might happen when a particular object receives a given message, since the behaviour of an object is influenced by the values of its attributes. A state machine tells you about the life history of an object. Therefore you can use the frequency of change of an attribute during an object’s lifetime as a means of choosing which attributes are significant when constructing a state machine. In the class Copy, for example, the attribute returnDate is likely to change often, as it reflects the transition between the states on shelf and on loan. However, attributes such as libraryNumber and classification will only change as a result of some reorganisation within the library (probably set during the creation of instances of the class Copy). The borrowing and returning of each copy by the library’s members do not affect them. Among other things, you might consider an additional attribute that records the actual date when a copy is returned (actualReturnDate, say) if you needed to investigate the notion of fines for overdue books.
(c) You might need to develop state machines retrospectively. For example, you might benefit by preparing a state machine when testing individual classes, to demonstrate that the behaviour of an object over its lifetime satisfies the relevant requirements for its class – usually for the purposes of verification. In addition, a proposed change to a software system might introduce a need to prepare one or more statechart diagrams to show how an object’s state would be affected.
It is possible to have both entry and exit actions on a single state. Suggest a potential benefit to the consistency of your implementation as a result of using entry and exit actions in pairs on the states of an object.
Entry and exit actions cannot be bypassed, and therefore can be used to tidy up the states within an object and maintain a level of consistency. An entry action can be used to perform the setting up required before the object enters a state. Similarly, an exit action can be used to ‘clean up’ afterwards.
(a) In Section 4 you saw examples of message passing in state diagrams. In UML, what kind of event is a message?
(b) Is a signal a class? Briefly explain your answer.
(c) What is the difference between a time event and a change event?
(d) What kinds of event are entry and exit events?
(a) A message is the commonest case of a call event.
(b) A signal is a special kind of class. However, there are no operations associated with a signal, and its parameters are recorded as attributes. Because they are classes, signals may be related through an inheritance hierarchy, but they must be separated from the other classes in the software system.
(c) A time event relates to a specific point in time or an interval. A time event is a Boolean test of a time expression, such as the length of time that must elapse between a named event and a subsequent action. A change event takes place when a test condition evaluates to true at some point in time. A change event is not restricted to time; rather, the transition can fire when some condition is true, such as the level of liquid in a storage tank reaching some predetermined danger level.
(d) They are both signal events, because they happen when a state transition occurs for whatever reason, such as a call or a time event. An entry event sets off an action sequence when a message causes a state to be entered. An exit event sets off an action sequence when a message causes a state to be left.
(a) What is a composite state in UML? Must composite states have initial and final states?
(b) Consider the in library composite state for the class Copy in Figure 30. A composite state contains one or more substates, but does the outer state change while the internal state machine is working? Use the example to explain your answer.
(a) A composite state is one that contains some internal behaviour, which can itself be represented as a separate state diagram. It is not compulsory to show an initial and a final state. However, you should use an initial state to show which substate is the default entry point.
(b) No. The outer state represents the condition of being in any one of the substates. When the in library state is entered, the Copy object will be in one of the substates shown in Figure 30. When a Copy object is in a substate such as on shelf, it is, by definition, also in the in library state. When the Copy object leaves a substate, it leaves the in library state.
What characteristics of events suggest that an object should respond to them when they occur?
An object should normally respond to:
- events that are external, such as those from a point of sale terminal;
- change or time events that require some response;
- certain changes such as high/low temperature, in process-control systems, to avoid problems or even disasters.
Figure 22 shows a statechart diagram for an object of the class Copy in the lending library example. How might the diagram be amended to show that a copy becomes overdue if it is not returned within three weeks?
You can modify the statechart diagram in Figure 22 in a number of ways. The Copy object must still be in the on loan state. There are several choices, each one depending on a given library’s rules for overdue books.
For example, if it is not necessary to record an overdue book because there is no penalty attached to it, you can use an internal transition or a self-transition that is triggered by a time event: after(3 weeks) / notify member. Which kind of transition you use would depend on the existence of any entry and/or exit actions.
If there is a penalty for being overdue, you might use a composite state for on loan that requires at least one substate, overdue, which is entered by a change event: when (currentDate – issueDate > 3 weeks). Alternatively, you may defer calculating any fine until the book is returned, which would lead to an increase in the number of guarded transitions that respond to the return event.