Unit 6 - Interactions Flashcards
What are preconditions, postconditions and invariants collectively known as?
They are known as assertions, which are statements (in theory at least) either true or false
(a) 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?
(b) Explain why pre-and postconditions express a contract between a client object and a supplier object.
(c) What is meant by the term Design by Contract (DbC)?
(a) Assertions were used for placing constraints on the relationships between classes.
(b) 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.
(c) DbC is the process of developing software based on the notion of a contract between objects.
What is the important feature of DbC, which shows that it can be used to improve the quality of a software system?
DbC allows the development of a software system to be traced from requirements through to code.
What is the relationship between the client’s and supplier’s obligations and benefits?
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.
(a) What does weakening a precondition mean in terms of the provision of a service?
(b) Similarly, what does strengthening a postcondition mean?
(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.
(b) 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.
(a) Under what circumstances is one object, obj1 of class A, say, substitutable for another, obj2 of class B, say?
(b) What three sets of items should you examine to help you find suitable postconditions when identifying the possible operations for a class?
(c) Figure 1 (p13) 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) Class A must be a subclass of class B, and class A must respect all contracts agreed to by class B.
(b) 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.
(c) 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.
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) The obligations and benefits are shown in Table 3 (p14).
(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.
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 attribute of a loan (to enable you to deal with charging for overdue books at some later stage of the development).
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 ofthe 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.
(a) What is the difference in emphasis between sequence diagrams and communication diagrams?
(b) How is time represented in a communication diagram?
(c) What obligation is placed on an object that is sent a message?
(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.
(b) Time is represented by the sequential numbering of messages (see Section 4).
(c) The class of the receiving object is committed to implement an operation with a particular name and argument signature.
(a) How does the use of a pair of object diagrams help you prepare to build a sequence diagram?
(b) Is the initial message on an interaction diagram always sent from an object representing the user interface?
(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.
(b) 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.
What does GRASP stand for?
General Responsibility Assignment Software Patterns
(a) In an interaction diagram, which class must provide the operation indicated by a message passed from one object to another?
(b) What does a lifeline represent?
(c) What does the box at the top of a lifeline include?
(d) What sort of arrowhead is used on an arrow depicting synchronous messagesending?
(e) What is a procedural interaction? With what might it be contrasted?
(f) In a sequence diagram, what does the widening of a lifeline into a tall, thin rectangle mean?
(a) The class of the receiver object must provide the appropriate operation.
(b) A lifeline represents the portion of the life of an object covered by the sequence diagram.
(c) 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.
(d) 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.)
(e) 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.
(f) 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.
(a) What does it mean to delegate in the context of object-oriented design?
(b) Would you describe a sequence diagram as a programming notation?
(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).
(b) 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.
Imagine constructing some sequence diagrams for checking out a guest. What new classes might suggest themselves as candidates for inclusion in the class model?
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.
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?
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.