protocols software engineering ii wirfs brock et al, designing object-oriented software, prentice...

18
Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, 1990. Mitchell, R., and McKim, Design by Contract, by Example, Addison-Wesley, 2002 La Trobe University, Data Structure, http://ironbark.bendigo . latrobe.edu.au/courses/subjects/DataStructu res/mal/session070/lecture.html

Upload: erik-campbell

Post on 18-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, 1990. Mitchell, R., and McKim, Design by Contract,

Protocols

Software Engineering II

Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, 1990.

Mitchell, R., and McKim, Design by Contract, by Example, Addison-Wesley, 2002

La Trobe University, Data Structure, http://ironbark.bendigo. latrobe.edu.au/courses/subjects/DataStructures/mal/session070/lecture.html

Page 2: Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, 1990. Mitchell, R., and McKim, Design by Contract,

2

Goals of Protocols• Refine design

– – Associate one or more methods (procedures) with

each responsibility

• Define protocols– Protocols

• set of signatures for methods to be implemented

– Signatures• method name, input and output parameters, and return

type

Page 3: Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, 1990. Mitchell, R., and McKim, Design by Contract,

3

Importance of Protocols• Delegate implementation to programmers

– Specialization of personnel (analyst, designer, programmer)

• Program understanding– Maintenance

• Test Cases– Derive test cases from pre and post conditions

• Documentation–

Page 4: Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, 1990. Mitchell, R., and McKim, Design by Contract,

4

Protocol Structure• Signature

– Method name– Type of input and output parameters– Description of input, output, input-output parameters– Description of internal data structures

• Purpose• Pre-conditions // require• Post-conditions // ensure • Uses

Page 5: Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, 1990. Mitchell, R., and McKim, Design by Contract,

5

Properties• All ADTs and objects possess properties

(behavioral rules).• Properties can be stated using a set of Pre- and

Post-conditions for each method.• Properties can be stated using a set of invariants

for each object or ADT.• Good software engineering practice recommends

that you to state Pre- and Post-conditions for all methods.

Page 6: Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, 1990. Mitchell, R., and McKim, Design by Contract,

6

Pre- and Post-conditions• Provide a set of conditions (a contract) that

the implementer of the method must meet

• Help the reader to know what they can expect of the method

Page 7: Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, 1990. Mitchell, R., and McKim, Design by Contract,

7

Pre-conditions• 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 a statement that is true or false• May consist of statements connected by logical

operators (AND, OR)• • Can be written formally or informally

Page 8: Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, 1990. Mitchell, R., and McKim, Design by Contract,

8

Post-conditions• Must clearly state what is true when the

method completes execution

• Should be strong enough so that only correct implementations will satisfy the condition

Page 9: Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, 1990. Mitchell, R., and McKim, Design by Contract,

9

Notes

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

• Why?

• Common to discover holes in design at this point

• Repeat earlier phases of design

Page 10: Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, 1990. Mitchell, R., and McKim, Design by Contract,

10

Example: ADT Stack -1

• 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 FILO (First In Last Out) list.

Page 11: Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, 1990. Mitchell, R., and McKim, Design by Contract,

11

Example: ADT Stack -2

• Public operations– Initialize– – Pop– IsEmpty– IsFull– Length

Page 12: Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, 1990. Mitchell, R., and McKim, Design by Contract,

12

Example: ADT Stack -3

• IsEmpty() : boolean – PRE:

– POST: •

• The stack is unchanged

Page 13: Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, 1990. Mitchell, R., and McKim, Design by Contract,

13

Example: ADT Stack -4

• pop (): object – PRE:

– POST: • If stack is not empty, then return the item from the top of the stack

• The length of the stack at the end of the operation is one less than the length of the stack at the start of the operation

• If stack length is greater than one, then the top of the stack is repositioned to the penultimate item in the stack.

• The remainder of the stack is unchanged.

Page 14: Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, 1990. Mitchell, R., and McKim, Design by Contract,

14

Example: ADT Stack -4 (revisted)

• pop (): object – PRE:

– POST: (If stack is not empty, then

(the item from the top of the stack is returned ANDthe length of the stack at the end of the operation is one less than

the length of the stack at the start of the operation ANDthe remainder of the stack is unchanged)) AND

If stack length is greater than one at the start of the operation, then the top of the stack is repositioned to the penultimate item in the stack.

Page 15: Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, 1990. Mitchell, R., and McKim, Design by Contract,

15

Example: ADT Stack -5

• push (IN: item) : void – PRE:

• The stack has been initialized and the stack is not full.

• item contains valid data.

– POST: •

• The top of the stack contains the object passed in as item.

• The remainder of the stack is unchanged.

• The stack is not empty.

Page 16: Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, 1990. Mitchell, R., and McKim, Design by Contract,

16

Example: ADT Stack -6Robustness

• push (IN: item) : void – //Adding to the stack

PRE: // relaxing preconditions

• The stack has been initialized.

– POST: • The size of the stack is incremented by one. • The top of the stack contains the object passed in as item. • The remainder of the stack is unchanged. • The stack is not empty.

– Error conditions:• If the stack is full,

then display error message AND do not change the ADT state• If item does not contain valid data,

then display error message AND do not change the ADT state

Page 17: Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, 1990. Mitchell, R., and McKim, Design by Contract,

17

Specifying Protocols

• Include in Detailed Design section of SDD• For each subsystem, group documentation of classes as

follows:Class: <name>Superclasses: <name> || noneSubclasses: <name> || noneCollaboration Graphs: See Figure <figure number>Description: <class descriptions>Contracts: <list of contracts with brief summary>Private variables: <type> <variable name> <brief description> […

<type <variable name> <brief description>] || noneSee handout for method template

Page 18: Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, 1990. Mitchell, R., and McKim, Design by Contract,

18

Exercise: ADT List

List (void); bool isin (int item);void print (void);void count (void);void print_reverse (void);void insert (int item); void delete(int item);

Write the description and pre- and post- conditions for the following methods: