Unit 6 Flashcards

1
Q

In the analysis and design phases of system development that you studied in Units 3 and 5, what were the assertions (preconditions, postconditions and invariants) used for?

A

Assertions were used for placing constraints on the relationships between classes.

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

Explain why pre-and postconditions express a contract between a client object and a supplier object.

A

The contract is expressed by: The precondition requiring something from the client object, which is of benefit to the supplier object;
The postcondition requiring something from the supplier object, which is of
benefit to the client object.

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

What is meant by the term Design by Contract (DbC)?

A

DbC is the process of developing software based on the notion of a contract between objects.

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

What is the important feature of DbC, which shows that it can be used to improve the quality of a software system?

A

DbC allows the development of a software system to be traced from requirements through to code.

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

What is the relationship between the client’s and supplier’s obligations and benefits?

A

A client’s obligations to constrain inputs provide benefits to a supplier in that not so many input cases need to be considered. A supplier’s obligations to produce outputs satisfying certain constraints mean that a client can expect to receive a clearly defined service.

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

What does weakening a precondition mean in terms of the provision of a service?

A

To weaken a precondition means generalising the situation in which a service can be provided. In general, this means that it is easier for the client to satisfy the precondition because there are ‘fewer’ conditions to be satisfied.

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

Similarly, what does strengthening a postcondition mean?

A

To strengthen a postcondition means making the service that is requested ‘better’ in terms of time, precision or some other measurable item. The precise notion of ‘better’ is not fixed, but must be considered in terms of the contract of which the postcondition is part. It can, however, make the postcondition more difficult for the supplier to satisfy because there are ‘more’ conditions to be satisfied.

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

Under what circumstances is one object, obj1 of class A, say, substitutable for another, obj2 of class B, say?

A

Class A must be a subclass of class B, and class A must respect all contracts agreed to by class B.

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

What three sets of items should you examine to help you find suitable postconditions when identifying the possible operations for a class?

A

You should investigate the following three sets of items when searching for the possible postconditions for an operation:
Instances of a class (its objects) that have been created or deleted;
Instances of associations (links) that have been formed or broken;
Attributes that have been modified.

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

Figure 1 shows a class model for the lending of books where there is a requirement to record both past and current loans. Describe the contract to borrow a book in terms of objects and links created. Your answer should differentiate between the pre-and the postconditions.

A
The contract between the library member and the library to borrow a book is constrained as follows.
Precondition: there must be an instance of the class LibraryMember that corresponds to the real-world member; there must be an instance of the class Book that corresponds to the real-world
book that the member wants to borrow; the instance of the class LibraryMember must be linked to fewer than 3 instances of the class Loan in the role of currentLoans. Postcondition: a new instance of the class Loan will have been created; the instance of the class LibraryMember will have been linked to the new instance of Loan in the
role of currentLoans; the instance of the class Book will have been linked to the same new instance
of the class Loan.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Consider the contract for a premium-rate, 24-hour courier service in which compensation is paid for non-delivery within 24 hours.

(a) Show in a table the obligations and benefits of the client and supplier of this service.
(b) Give the pre-and postconditions.

A

a) The obligations and benefits are shown in Table 3 page 14.
(b) The pre-and postconditions are as follows. Precondition: the service is paid for. Postcondition: either the package will have been delivered within 24 hours, or compensation will have been paid.

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

Look at Figure 1 again, and identify the pre-and postconditions for returning a book to the library. Include the fact that there are two significant dates for each loan in relation to the possible payment of fines:
An expected return date;
An actual return date.
Assume that the value of the expected return date will have been set as part of borrowing a book. Also assume that the actual return date will be recorded as an attributeofa loan (to enable you to deal with charging for overdue books at some later stage of the development).

A
In Figure 1, the class Loan has two associations with the class LibraryMember, to differentiate between current and past loans. The contract for returning a book can be expressed as follows.
Precondition: the instance of the class LibraryMember is linked to an instance of the
class Loan in the role of currentLoans;
Postcondition: the link between the instance of the class LibraryMember and the instance of the
class Loan in the role of currentLoans will have been broken;
the instance of the class LibraryMember will have been linked to the same instance of the class Loan in the role of pastLoans;
a value will have been set for the attribute actualReturnDate in the same instance
of class Loan.
Note that the association between the class Loan and the class Book is not affected by returning a book. The instance of the class Book is linked to the instance of the class Loan before and after its return.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the difference in emphasis between sequence diagrams and communication diagrams?

A

Sequence diagrams emphasise the flow of messages from object to object over time. Communication diagrams emphasise the message traffic across the links in a particular configuration of objects.

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

How is time represented in a communication diagram?

A

Time is represented by the sequential numbering of messages (see Section 4).

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

What obligation is placed on an object that is sent a message?

A

The class of the receiving object is committed to implement an operation with a particular name and argument signature.

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

How does the use of a pair of object diagrams help you prepare to build a sequence diagram?

A

The aim is to show how a given postcondition can be achieved in a sequence diagram. A pair of object diagrams, showing the states before and after the operation in question, identifies the changes in system state that take place in order to meet the postcondition.

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

Is the initial message on an interaction diagram always sent from an object representing the user interface?

A

No – we are not constrained to showing interactions with the user interface. Message sequences can originate from any object. The user interface is the origin for those messages that relate to a use case scenario, which we have described in the case of checking guests in to a hotel. However, interaction diagrams can become very complex if we try to show all the possible messages for a given configuration of objects. Following the principle of modularisation, we would split up a complex interaction into a number of smaller ones. In the new diagrams, the starting point need not be the user interface.

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

In an interaction diagram, which class must provide the operation indicated by a message passed from one object to another?

A

The class of the receiver object must provide the appropriate operation.

19
Q

What does a lifeline represent?

A

A lifeline represents the portion of the life of an object covered by the sequence diagram.

20
Q

What does the box at the top of a lifeline include?

A

It includes an instance name, optionally followed by a colon and a class name, or, for a generic object,just a colon and a class name. For example, objectname, objectName : ClassName and : Classname are all allowed.

21
Q

What sort of arrowhead is used on an arrow depicting synchronous message sending?

A

A solid black arrowhead, which indicates that the interaction is procedural. Note that an open or stick arrowhead with a dashed shaft is used to signify the method’s return, although it is often omitted for clarity. (See Unit 7 for a list of permitted arrow styles.)

22
Q

What is a procedural interaction? With what might it be contrasted?

A

It is an interaction in which the sender of a message is blocked until the receiver of the message has finished processing. This is exactly the same as what is sometimes called subroutine semantics. It is the usual policy when a single thread of control is allowed. If multiple threads are allowed, we might not want the sender of the message to block, in which case we could start a concurrent activity.

23
Q

In a sequence diagram, what does the widening of a lifeline into a tall, thin rectangle mean?

A

This shows that the object is active. An object is said to be active if it is either performing an operation or awaiting completion of an operation that it has requested another object to perform.

24
Q

What does it mean to delegate in the context of object-oriented design?

A

One object is said to delegate behaviour to another when instead of implementing some behaviour, it sends a message to another object that implements that behaviour. We considered the possibility that instead of the Hotel doing all the work of finding a free room and housing jill in it, the Hotel might delegate the work to a Room by passing it the message attemptToAccommodate(jill).

25
Q

Would you describe a sequence diagram as a programming notation?

A

Not really, because all it shows is the inter-object message traffic. It does not represent the algorithms needed by senders in order to decide when and to which objects messages should be sent, or those needed by receivers in order to act on the messages. When you have finished drawing sequence diagrams, you know the interfaces of the various classes, but not how those interfaces will be implemented.

26
Q

Construct another sequence diagram for checking jill in to theRitz,in which theinitial message from the userinterface is sent to theRitz. Then complete the sequence diagram by sending the second message to jill. Use the list of classes and associated methods given in Table 5. Why must the first message go to a Hotel objectrather than a Guest object?

A

Our sequence diagram for checking jill in to theRitz is given in Figure 8. As you saw in Unit 4, guests are given a reservation number to identify themselves when checking in, and the sequence diagram in Figure 6 shows how that reservation number is used to identify the Guest object in the software system. In the alternative interaction shown in Figure 8, we still need to identify the Guest object before that object can be used to complete the process of checking in.

27
Q

Imagine constructing some sequence diagrams for checking out a guest. What new classes might suggest themselves as candidates forinclusion in the class model?

A
Checking out requires the completion of a Bill, which might contain a number of separate items. A bill relates to the length of the stay, which has not been modelled. Perhaps we need attributes to record arrival and departure. These might fit most naturally in a Stay class. Payments may or may not be settled immediately, so it is wise to separate a Bill from any Payment that might arise. Collected instances of Bill and Payment might be naturally grouped into instances of an Account class.
All the above are extra classes that might have arisen when developing the use cases, but might arise later in the design process. Classes that are inherently design classes, which could not arise from writing use cases, might include a CheckerOut class to encapsulate the procedures for checking out, or a RoomAllocator to centralise decisions on how the set of free rooms is to be handled.
28
Q

The sequence diagram in Figure 6 shows the userinterface sending a message checkIn (“res23”)to theRitz. How might the userinterface discover that this is the correct hotel to send the message to?

A

This would be handled in another use case, probably log on, shown in Figures 18 and 19 of Unit 3. (This is the use case performed by the receptionist on start-up, in which they identify the hotel that they are serving.)
This illustrates that though you may think of issues when developing a particular use case, those issues may be properly resolved in other use cases.

29
Q

How is the sequencing of messages represented in a communication diagram?

A

Every message has a multi-stage number. The numbers specify the sequencing, replacing vertical position in a sequence diagram.

30
Q

Sequence diagrams and communication diagrams show almost equivalent information. What are their respective strengths and weaknesses?

A

Sequence diagrams make the relative order of messages extremely easy to see by presenting time vertically. Communication diagrams are an extension of object diagrams, so there is less new notation. They make it easy to see the links and to show role names, at the cost of making the relative ordering of messages less immediately clear.

31
Q

How does an assignment statement help construct a prototypical sequence diagram?

A

In a programming language, an assignment statement allows you to store the result of one message in a variable and then send messages to whatever object is currently referenced by that variable. You can use the same mechanism in UML, although the name used to store the message result is not the same as a programming variable. The name can then be used as a parameter to other messages. In a bank, for example, you might want to credit an account with an amount of money. You can use a name such as ac to store a reference to an Account object (at a particular branch), and then send a message, such as credit(amount), to that object.

32
Q

Figure 14 shows UML notation for creating new Guest objects in a communication diagram. Has this cured the problem of apparently sending messages to objects that do not yet exist?

A

No – it is still a notation based on message sending. As Figure 14 shows, marking the instance {new} has not captured exactly when it comes into existence. We are still dependent on the pseudo-message newGuest() to express creation.

33
Q

How does a sequence diagram drawn for a use case scenario differ from one drawn to show how an internal operation, such as findAFreeRoom in Figure 12, is carried out?

A

A sequence diagram of a use case scenario will always have the very first message originating from an object which has been stimulated, directly or indirectly, by an actor (usually a user interface object). An internal operation will be invoked by a message that has been identified in the use case scenario. You can show how the recipient of that message achieves the required behaviour (how a free room is found, for example). In all other respects they are the same.

34
Q

Other than the simple realisation of a use case scenario, what are the other uses of interaction diagrams?

A
An interaction diagram can be drawn at any level of detail for a software system; use cases are just one example. Other uses of interaction diagrams are as follows.
They show how a class provides an operation. This is sometimes called a subcommunication. An example might be documenting how findAFreeRoom works in the hotel example of Figure 6, as this is just one operation on a class rather than a particular use case scenario.
They show how a component can be used. A component might need various operations to be invoked in a particular sequence in order to achieve some goal. An interaction diagram can document a typical interchange of messages.
They show how a design pattern works. We described the possibility of a Hotel delegating its room allocation to a RoomAllocator class, and mentioned that this is a case of the Strategy pattern in which behaviour is moved out to a separate object.
Alternatively, we could describe this pattern in general terms using a sequence diagram. The instances would be general (prototypical) objects with names such as theServer and theDelegate.
35
Q

Would you expect to use sequence diagrams in a conceptual model?

A

Sequence diagrams are about message passing between software objects, and conceptual models are about things in the world, where the language of message passing makes no sense.

36
Q

Recall Exercise 3, where you were asked to model the checking in sequence. Using the multi-stage numbering scheme, draw a communication diagram equivalent to the sequence diagram in Figure 8.

A

Our solution is shown in Figure 15 page 40.

37
Q

Use Figure 16 to represent a prototypical interaction for the borrowing of a book, which you saw in SAQ 5(c). Draw both a sequence diagram and a communication diagram that sends the message borrow(b)to aninstance of LibraryMember from aUserInterface, where b is a reference to the object representing the book that the library member m wants to borrow. At this point we are not concerned about where the object b came from.

A

We can assume that the precondition for borrowing a book, which was given in the solutionto SAQ 5(c), has been established. Our aim for each interaction diagram is to show that the corresponding postcondition has been met. Figure 17 shows the sequence diagram, and Figure 18 shows the communication diagram, page 41.

38
Q

What are the advantages of recognising when an association is unidirectional?

A

Representing an association in just one directions simplifies the implementation of the classes at each end and avoids the need to worry that both ends of the link are consistent when dealing with instances of those classes. But note that the overall flexibility is reduced in comparison with bidirectional associations.

39
Q

Suppose that in a Java implementation, a Company class represents the employment association with the Person class as a Vector of Person objects. What would be the disadvantage of providing a method Company::getAllEmployees that returned the Vector?

A
If Company::getAllEmployees is defined as returning an instance of the class Vector, changing the internal representation creates either a maintenance problem or a data type conversion problem. Suppose the internal representation is changed to be an array. You then have to decide whether getAllEmployees should be changed to return the array (affecting all existing clients of the class), or a new Vector should be constructed from the array.
In addition, whichever Java class is used, encapsulation could be breached because some other object could modify the Vector object without using the methods supplied by the class Company, creating false links (the links would be in the copy of the Vector but not known by the Company). If clients of Company need to iterate over all employees, an interface should be provided that allows iteration without revealing internal implementation decisions, and that has defined semantics for what happens if the links change during iteration.
40
Q

According to the Law of Demeter, an object should only send messages to a certain set of objects including the object itself. List the other objects.

A

The Law of Demeter allows an object to send messages to:
any objects communicated as parameters of the current method;
any new objects that the object has created in the current method;
any objects to which the object has direct links – its neighbours;
itself.
Note that the law does not allow sending messages to objects that are returned as a result of sending other messages.

41
Q

In Figure 23, which pattern would be outlawed by the Law of Demeter?

A

The Fork pattern would be outlawed. Here, it would involve sending a message to the Job to get a Person, and then sending messages to the resultant Person object. The law bans the sending of messages to objects that are returned as a result of sending other messages. The Cascade implementation involves the Company talking only to Job, and Job talking only to Person. Both of these are direct associations, so the Law of Demeter will allow this. One advantage of using the Cascade pattern is that the Company is independent of how the age of an employee is represented. You could store the age directly within Job, or you could store it in an associated Person object. The Company will be oblivious to such choices.

42
Q

Figure 24 shows a class model for a Company. Notice the directionality of one of the associations. For each association, decide which end should manage the association, and what methods must be defined. Mark each new method to show whether it is publicly available or just a helper method to assist the implementation of a public method.

A
In Table 6, we have decided that Company should be responsible for the Company–Department link. Because the link is unidirectional, we need methods only on the Company end.
The Department–Employee association is bidirectional. We have decided to make Department responsible, so the only methods to manipulate links intended for public consumption are those on the class Department. However, the methods in Department will need the assistance of a helper method on Employee.
43
Q

Figure 25 shows a fragment of a hotel system, extended to give each Guest a Bill. Suppose we wish to implement a use case printBill(Guest jack). It has been decided that the user interface will send the initial message printBill(jack)to the Hotel.
Draw two sequence diagrams, showing fork and cascade solutions. For each solution,
list the methods on each class that this use case requires.
Which of your solutions would the Law of Demeter exclude, and why?

A

Figures 26 and 27 show the two sequence diagrams. For the fork solution, the methods required are shown in Table 7. Table 8 shows the methods for the cascade solution. The guidelines identified within the Law of Demeter would exclude the fork solution, because it involves the Hotel sending the message print()to the Bill. The Bill is not an object known to the Hotel through a permanent association; it is neither created in this method of the Hotel, nor passed to the Hotel as a parameter to the current method. Page 52.