111 Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, 1990....

25
1 Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, 1990. (Chapter 8) Meyer, B., Applying design by contract, Computer, 25(10):40-51, October 1992.

Transcript of 111 Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, 1990....

111

Protocols

CS 4311

Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, 1990. (Chapter 8)

Meyer, B., Applying design by contract, Computer, 25(10):40-51, October 1992.

222

Outline

Protocols: what and why? Documenting protocols Process and guidelines

Review

Responsibility?

Contract?

3

Definitions

Signature: method name, input and output parameters and their types, and return type

Protocol: set of signatures for methods to be implemented

4

Why Write Protocols?

Define an interface to a class.

(Why?)

5

Protocol Structure

Signature Method name Type of return value Type of input and output parameters Description of input, output, input-output

parameters

Purpose Pre-condition Post-condition

6

Notation

Signature, a.k.a. syntactic interface Language neutral declaration, e.g., UML and CRC

draw(in g: Graphics): void Language-specific declaration, e.g., Java and C++

void draw(Graphics g)

Strength and weakness?

7

Notation (Cont.)

Behavior, a.k.a. semantic interface Informal description or stylized texts

Example: Given a non-empty list of integers, determine if it is sorted.

Formal descriptions, e.g., OCL and Design-by-Contract notationspre: not c->isEmpty()

post: c->incldues(result) and c->forAll(e: Integer | result >= e)

Strength and weakness?

8

Specifying Classes

Class: DrawingSuperclasses: DisplayableObjectSubclasses: NoneClass Diagram: see Figure 2-1Collaborations Diagram: see Figure 3-5Description: Represents the structure of the elements …Contracts2. Maintain the elements in a drawing Know which elements are contained in the drawing

addElement(DrawingElement)uses List

Adds a drawing element to the front of the list of drawing elements.

Pre: The element is not already contained in the list.Post: The element is the first element of the list.

9

Specifying Classes (Cont.)

elementAt(Point) returns DrawingElement uses List, DrawingElement (3)

Returns the drawing element at the given point.Pre: nonePost: if boundary rectangle contains Point then

returned is the first such drawing element in List else returned is a null object

Maintain the ordering between elementselementAfter(DrawingElement) returns DrawingElement uses List …

10

Pre-condition Capture the conditions that must be true

before the method executes Describe the required state of the ADT or

object before entering the function Written as an expression that is true or false May consist of expressions connected by

logical operators (AND, OR) Use true when no pre-condition exists

11

Post-condition Must clearly state what is true when the

method completes execution Describe the expected state upon exiting the

function Should be strong enough so that only correct

implementations will satisfy the condition

12

What’s the Value of Pre- and Post-conditions? (Pair) 2 minutes

13

Process for Writing Protocols

For each classFor each contract

For each responsibility

Specify complete protocol (set of signatures) to support the responsibility

14

Guidelines: Names

Use single name for single conceptual operation, regardless of where it is found E.g., add vs. insert, remove vs. delete, contains

vs. has vs. includes for collections

Associate a single conceptual aspect with each method name

15

Guidelines: Generality

Make protocols as generally useful as possible The more general a responsibility, the more

messages needed to support it. E.g., size vs. enumerating elements for collections

16

Guidelines: Default Values

Define reasonable defaults Provide most general message: user specifies all

possible parameters Provide defaults for any parameter for which it is

reasonable Let client specify subset of parameters

17

Example

display() display(device) display(region) display(device, region) display(device, region, rule) display(device, region, rule, transform)

18

Exercise

Make the indexOf method of String more general and reusable.

/** Returns the index of the first occurrence of the given character. */

int indexOf(int ch)

(Pair) 2 minutes

19

Guidelines: Pre and Post

State as assertions not as actions. Why? E.g., int String.indexOf(int c) pre: none

post: if c appears in this string then return the index of the first occurrence

else return -1

post: if c appears in this string then result is the index of the first occurrence of c else result is -1

post: result is the index of the first occurrence if c appears in this string; otherwise it is -1

20

Guidelines: Rework, rework, …

Generate protocols for main responsibilities Protocols to public methods must be unambiguous

since it is the interface with clients Protocols to private methods are notes to developer

Common to discover holes in design at this point Repeat earlier phases of design

21

Example: ADT Stack Additions are restricted to one end identified as

the top of the stack. Deletions are restricted to the top of the stack. Only the item at the top of the stack is accessible A stack is often referred to as a LIFO (Last In

First Out) list.

22

In-class

Identify operations for Stack (Pair) 2 minutes

23

In-class

Specify protocols for isEmpty() and pop() (Pair) 5 minutes

24

In-class (Pairs)

Consider a class, say Contact Manager, that manage a contacts list, where a contact contains contact information such as name, phone number, email address, etc. Identify responsibilities for Contact Manager Define protocols for adding and search

operations/responsibilities.

25