Applying Statechart Diagram to Object Design - Quiz Explanation
The correct answers are indicated below, along with text that explains the correct answers.
1.
What features of a statechart diagram map to operations or operation implementations in the class diagram?
Please select the best answer.
A.
Activities describe functions that are built into the object. These functions are behaviors that are specific to the state in which they are defined.
B.
Events, actions, and activities map to operations or operation implementation logic.
C.
Actions specify the response to an event which typically maps to the implementation of the operation interface defined by the event signature.
D.
Events map to the public operations that define the interface to the object.
The correct answer is B.
Events map to public interfaces. Actions define the implementation of events so they map to either implementation logic for a public interface or sub-operations invoked by an interface operation. Activities are operations that the object invokes while it is in the state in which the activity is defined. A, C, and D are incorrect because an activity, action, and event are each simply one of the features that maps to an operation or implementation.
2.
What causes a change in an object's state?
Please select the best answer.
A.
Activities trigger events in the course of performing the internal object behaviors. Therefore, activities cause a change in state.
B.
Events trigger activities that alter the values of the state attributes, therefore redefining the state of the object.
C.
Actions trigger activities that alter the values of the state attributes, therefore redefining the state of the object.
D.
An event triggers a state transition. An event action performs the actual change to state attributes.
The correct answer is D.
An event triggers a state transition. An event action is the object response to the event that performs the actual change to state attributes. A is incorrect because activities describe work that an object performs and typically would only trigger send events (events that are sent to other objects). B is incorrect because events trigger actions, not activities. C is incorrect because actions do not trigger activities. Being in a particular state initiates activities.
3.
How is the state of an object represented during the execution of the application?
Please select the best answer.
A.
Object state corresponds to a unique event. Whenever the object receives a specific event, the object is set to the corresponding state.
B.
Object state is represented by the values of one of an object's attributes.
C.
Object state is represented by the values of one or more of an object's attributes.
D.
Object state corresponds to the state of a given activity. An object changes state to reflect the status of the activity from initiation to completion.
The correct answer is C.
The state of an object is represented by the values of one or more of the object's attributes. Sometimes a programmer may consolidate the unique combination of values into a single flag or status code, but the actual state may represent a unique combination of any number of attributes. A is incorrect because any given event may cause a different state change depending on the current state of the object when the event occurs. B is incorrect because a state may be represented by the values of more than one of the object's attributes. D is incorrect because activities do not affect object state. They are simply work that the object performs while in a particular state.
4.
What does delegation mean in the state design pattern?
Please select the best answer.
A.
The responsibility for implementing a behavior is passed to an object that represents the current state of the object.
B.
The responsibility for implementing a behavior is passed to an internal private operation within the object that represents the selected state.
C.
The responsibility for validating a behavior is given to a special function within the object.
D.
The responsibility for changing the object's state is given to another object that specializes in that particular kind of change.
The correct answer is A.
Delegation in the state design pattern means that the responsibility for implementing a behavior is passed to an object that represents the current state of the object. There is one object for every state that the object can assume. B is incorrect because delegation in any pattern passes the responsibility to another object and is not performed internally at all. C is incorrect because delegation does more than validate; it actually implements the behavior. Also, delegation always goes to another object, not just another operation. D is incorrect because the delegated behavior does not have to be involved with a state change, it may be any kind of behavior.
5.
How would you identify possible state changes in an object using a sequence diagram?
Please select the best answer.
A.
Look for outgoing events from the object's vertical timeline. These outgoing events represent a change in state.
B.
Look for event returns. The response to an event requires a change in state.
C.
Look for events coming into the object's timeline. Incoming events require a response that can cause a change in the object.
D.
Look for specific event names that imply change to the object. The send and return of each of these events corresponds to a unique state of the object, for example, send and wait, and evaluate return value.
The correct answer is C.
To identify possible state changes of an object, look for events coming into the object's timeline. Incoming events require a response that can cause a change in the object. Not every incoming event causes a state change, but every incoming event requires some kind of response from the object. A is incorrect because outgoing events are simply messages sent to other objects. Aside from the possible wait state while waiting for a reply, there is no state change associated with an outgoing event. B is incorrect because event returns only represent one type of incoming event. Any incoming event may cause a state change. D is incorrect because event names do not necessarily reflect the change in the target object. In fact, the event name may say nothing about the state change. Poor naming can confuse the matter even more.