Unit 7 - More on dynamic modelling - states and activities Flashcards

1
Q

UML has a more structured way of indicating conditional, alternative and iterative behaviour. This is based on the use of a more general notion of what
is called a fragment, which is sometimes referred to as a frame. What are some standard labels?

A

Opt, alt or loop, standing for optional, alternative or looping behaviour.

. Optional fragment. The behaviour specified inside the fragment is only executed if a guard, shown at the top of the box, is true

. Alternative fragment. This is used where we want to choose from a number of mutually exclusive pieces of behaviour. The box is divided, with alternatives in sub-boxes along with appropriate guards.

One sub-box can be labelled with the guard else, to indicate it contains the default behaviour. (Hence the opt fragment is like a simplified alternative fragment, with one guard and no default behaviour.)

. Loop fragment. The behaviour is repeated while a guard, shown in the top left-hand corner, remains true or a variable moves between two bounds.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why must the conditions on a message send be mutually exclusive in a sequential system?

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, resulting in multiple receiving objects being active at the same time – leading to the possibility of non-deterministic
behaviour.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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?

A

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();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Suggest a reason why an object’s lifeline must branch in cases such as Figure 2. How is this handled in Figure 5?

A

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 in Figure 2.

In Figure 5 the alternatives are shown using the alternative fragment. Although the figure doesn’t show a branching lifeline, this same branching is implied by the alternatives in the fragment, in their separate boxes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How would you change the sequence diagrams in Figures 3 and 6 to sound the car’s horn five times each time it moved forward?

A

To get the horn to sound five times for every forward movement in the sequence diagram in Figure 3 you would add an iteration clause to the second, nested message: *[j := 1..5] sound().

In Figure 6 you would add the extra iteration by including a fragment within the
existing fragment.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Give three strategies for implementing use cases.

A

One central class. Some designers like to minimise the knowledge the interface must have of the
business model by making the interface send all messages to a single object – usually some general object like a System or Hotel. In the case of the hotel
chain model each message could initially be sent to the Hotel, which would then forward the message to whatever object is to do the work.

Actor classes. Other designers are of the opinion that a clearer structure will result if the message is sent to a software object corresponding to the real-world actor who initiated the operation. For example, when a person arrives at a hotel they initiate the checking in process.

Use cases as classes. In a third strategy, interface messages are sent neither to a system object like a
Hotel nor to the object corresponding to an actor. Instead, a new class is defined for each use case. There might be classes such as CheckerIn,
CheckerOut and ReservationMaker. Each would have a method with a name like run, with a suitable number of arguments. The user interface would then create a single instance of the appropriate class, initialise it suitably and then send the run(…) message.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the main disadvantage of defensive programming?

A

The main disadvantage of defensive programming is that the same constraints within each precondition may be checked repeatedly in different operations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which of the three strategies for implementing use cases is being used when we place methods such as createGuest(lastname : String, firstname : String, address : String) in the Hotel class?

A

We are following the strategy of using one central class here.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Are use case objects consistent with the twin aims of high cohesion and low coupling?

A

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 introduces more coupling through the extra associations involved.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Suppose you are designing the graphical user interface (GUI) part of a hotel system. You need to be able to check in people known to the system as well as new people. What operations on the business model might be needed to support a usable GUI?

A

Checking in unknown people would involve the creation of a new Guest object, requiring an operation createGuest(firstName, lastName, address).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Recall the library system in Exercise 4 in Block 1 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
A solution can be found from the first of the three strategies discussed above. The messages sent from the user interface correspond to each of the library’s use cases. 
. reserveBook(…)
. borrowCopyOfBook(…)
. returnCopyOfBook(…)
. updateCatalogue(…)
. enrolNewMember(…)
. browseCatalogue(…).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a state machine?

A

It is a model that shows how an object changes from state to state in response to events.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What features are common to all state machine diagrams?

A

. states, represented by the round-cornered rectangles containing their names

. events, given as labels on transition arrows

. transitions, represented by arrows and identified by the events that give rise to them. Transitions occur as a response to an event, and cause a change of state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does a state machine model?

A

. its initial state when first created

. the different states it can be in while it exists

. how it can move from state to state in response to events – the state transitions that are possible.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is used in UML to represent a state machine’s initial state?

A

A solid black circle.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is used in UML to represent a state machine’s final state?

A

A black circle surrounding a solid black circle, to show that processing has been terminated.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Figure 15 shows a simple state machine diagram for the class Copy in a library model. What should happen if the copy returned message arrives when an object of the class Copy is in the state on shelf (assuming that we are not using design by contract)?

A

Something has gone wrong. The state of the system does not describe the fact that the copy of the book is on loan, so it cannot be returned. There must be some means of indicating or recording an error, such as a message to a log file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Give an example of an architectural decision that would provide a general solution to the problem of unexpected messages.

A
At an architectural level we might introduce a single object of a class Error, which is globally accessible to objects in the software system. 
Such an object would be responsible for reporting errors due to unexpected messages, for example.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What does a final state signify in a state machine?

A

Final states are used to show the point or points where the object in question has finished processing. Its activity has been completed.

20
Q

In what ways does a final state differ from an initial state?

A

There can be zero, one or more final states but at most one initial 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.

21
Q

The receipt of a message indicates what?

A

An event.

22
Q

Why might two objects of the same class respond differently to the same message?

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 might respond differently to the same message.

23
Q

What is the most common form of event that causes a transition between two states? How is it shown in a state machine diagram?

A

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 transition between the states.

In a state machine diagram, a transition is labelled with the name of the relevant message, which includes any arguments for that message.

24
Q

What is the difference between an event and an action?

A

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.

25
Q

What is the main constraint on the kinds of action that may be shown in a state diagram?

A

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.

26
Q

What is an action sequence?

A

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.

27
Q

In a state machine when is a [guard] evaluated?

A

When an event occurs

28
Q

What is a guard and how does it protect a transition?

A

A guard is a Boolean condition that is applied to a transition – the guard must be either true or false. A guarded transition can only take place when the specified guard is true.

29
Q

Look at Figure 17, which only considers whether an object of class Hotel is full. Why are there no guards on the transitions for the checkOut(aGuest) event?

A

A guest who is checking out cannot make the hotel full, so no guard is needed for the checkOut(aGuest) event.

30
Q

Why were guards introduced on the transitions for the checkOut (aGuest) event in Figure 18?

A

The example in Figure 17 is incomplete. Figure 18 deals with both extremes – when the hotel is full and when it is empty – as well as the possible configurations between full and empty. This results in checkOut(aGuest) occurring in three transitions, two of which (from not full to empty and from not full to not full) require guards to ensure mutual exclusion between them.

31
Q

List the two special actions for state machines in UML.

A

. an entry event, which is triggered every time an object enters a state
. an exit event, which is triggered every time an object leaves a state.

32
Q

What is an internal transition?

A

A special transition that does not involve a change of state. In contrast to a self transition,an internal transition does not cause the execution of any entry or
exit actions. An internal transition is typically used to handle an interrupt where you want some action to take place that does not affect the current state.

33
Q

What is an entry event, and how does it contribute to the maintenance of a state diagram?

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).

34
Q

What is the benefit of using an internal event as opposed to a self transition?

A

When there are entry and exit events that might interfere with a self transition, an internal event is useful because the entry and exit events are not triggered.

35
Q

What are some of the benefits of state machines?

A

. They are a means of elaborating the potential operations within a class – identifying which messages affect which attributes and considering the
consequences for each object belonging to a given class.

. They help you understand the behaviour of an object over its lifetime. The more often an attribute of an object changes, the more likely you are to want to model the object’s behaviour in a state machine.

. They help you understand how an object or a system must respond to events.

. They are a means of ensuring correctness.

36
Q

Identify three problems associated with complex state diagrams that might arise when designing classes.

A

Classes with complex state machines can cause three kinds of problem:
◦ it is harder to write the eventual code for such classes
because there are likely to be many conditional tests to
identify the actual state

◦ it is harder to test the classes because of the number of choices of pathway through the conditional tests

◦ 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.

37
Q

Suppose the class Copy included the attributes returnDate, libraryNumber and classification. Which of these attributes are significant for a state machine for the class Copy that has two states called on shelf and on loan? Explain your choice.

A

State machines help model what might happen when a particular object receives a given message because 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.

38
Q

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

There are situations where 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 state machine diagrams to show how an object’s state would be affected.

39
Q

What characteristics of events suggest that an object should respond to them when they occur?

A

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 a high or low temperature in a process control system, to avoid problems or even disasters.

40
Q

UML lets us describe five things about a transition. What are they?

A
The five parts of a transition are:
1 source state
2 target state
3 event trigger
4 action (or action sequence)
5 guard.
41
Q

Do packages contain only classes?

A

No – they can contain any model element from UML. Among other things, you can include classes, use cases, associations and even other packages.

42
Q

Which modelling principle would lead you to form packages?

A

You should use a package to raise the abstraction level, suppressing any detail in a particular diagram that is not necessary to understand what the model is showing.

43
Q

Explain how it is possible to have two separate elements that share the same name.

A

If you place the two elements in separate packages, they can share the same name. This is because each model element is identified by its name and the name of the smallest package that contains it.

44
Q

The stereotypes «import» and «access» allow you to ‘see’ through the ‘wall’ around a package. Which one can give rise to a naming problem? Which one provides the simpler traceability?

A

The «import» stereotype can give rise to a naming problem, because an «import» adds the names of the elements in the target package to the source package. The result is that, if an imported name clashes with an existing name, you have to qualify the imported elements with their package name in order to distinguish them. The «access» stereotype allows simpler traceability, because other packages must use the full name of an accessed element, such as PackageName::ElementName.

45
Q

Suppose you use «access» to ‘see’ into a package. Can you see everything inside that package? Briefly explain your answer.

A

There is no guarantee that you can ‘see’ all the elements of the package. It is possible to control the visibility of elements within a package in the same way that you can control the visibility of the attributes of a class. An element of the accessed package can be
marked as private, making it invisible to other packages that import this package and indicated with a ‘–’ character. Alternatively, it can be marked as protected, making it invisible to importers of the package but visible to child packages, and indicated with a ‘#’
character.