Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, 1990. (Chapter 8) Meyer, B., Applying design by contract, Computer,

Slides:



Advertisements
Similar presentations
11 Contracts CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 6)
Advertisements

Stacks, Queues, and Linked Lists
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
1 Specifying Object Interfaces. 2 Major tasks in this stage: --are there any missing attributes or operations? --how can we reduce coupling, make interface.
CHAPTER 3 COLLECTIONS SET Collection. 2 Abstract Data Types A data type consists of a set of values or elements, called its domain, and a set of operators.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Topic 3 The Stack ADT.
Chapter 3 Introduction to Collections – Stacks Modified
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
TECH Computer Science Data Abstraction and Basic Data Structures Improving efficiency by building better  Data Structure Object IN  Abstract Data Type.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
111 Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract,
CS Collaborations and Hierarchies CS 4311 Chapters 5 and 6 of Wirfs-Brock, R., Wilkerson, B., and Wiener, L., Designing Object- Oriented Software,
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
© 2005 Prentice Hall9-1 Stumpf and Teague Object-Oriented Systems Analysis and Design with UML.
CS Collaborations and Hierarchies CS 4311 Chapters 5 and 6 of Wirfs-Brock, R., Wilkerson, B., and Wiener, L., Designing Object- Oriented Software,
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Data Structures & Algorithms
Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, Mitchell, R., and McKim, Design by Contract,
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Linked Data Structures
Formal Specification.
Presentation of a paper on “Model-based Software Testing”
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
Introduction to Formal Methods
Review Array Array Elements Accessing array elements
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Used to help understand requirements more completely
GC211Data Structure Lecture2 Sara Alhajjam.
Data Abstraction: The Walls
Collaborations and Hierarchies
Homework 4 questions???.
Stacks.
Hassan Khosravi / Geoffrey Tien
About the Presentations
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Cinda Heeren / Geoffrey Tien
CSE 373: Data Structures and Algorithms
Algorithm and Ambiguity
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Abstract Data Types (ADTs)
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Preconditions, Postconditions & Assertions
Specifying Object Interfaces
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Slides by Steve Armstrong LeTourneau University Longview, TX
COMPUTER 2430 Object Oriented Programming and Data Structures I
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Introduction to Data Structure
Stacks Chapter 5.
Stack.
Algebraic Specification Software Specification Lecture 34
Chapter 5 Stack (part 1).
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
A type is a collection of values
CHAPTER 3: Collections—Stacks
Presentation transcript:

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

Outline Protocols: what and why? Documenting protocols Process and guidelines 2 2

Review Responsibility? Contract?

Definitions Signature: method name, input and output parameters and their types, and return type Protocol: set of signatures for methods to be implemented

Why Write Protocols? Define an interface to a class. (Why?)

Protocol Structure Signature Purpose Pre-condition Post-condition Method name Type of return value Type of input and output parameters Description of input, output, input-output parameters Purpose Pre-condition Post-condition

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?

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 notations pre: not c->isEmpty() post: c->incldues(result) and c->forAll(e: Integer | result >= e) Strength and weakness?

Specifying Classes Class: Drawing Superclasses: DisplayableObject Subclasses: None Class Diagram: see Figure 2-1 Collaborations Diagram: see Figure 3-5 Description: Represents the structure of the elements … Contracts 2. 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.

Specifying Classes (Cont.) elementAt(Point) returns DrawingElement uses List, DrawingElement (3) Returns the drawing element at the given point. Pre: none Post: if boundary rectangle contains Point then returned is the first drawing element in List else returned is a null object Maintain the ordering between elements elementAfter(DrawingElement) returns DrawingElement uses List …

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

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

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

Process for Writing Protocols For each class For each contract For each responsibility Specify complete protocol (set of signatures) to support the responsibility

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

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

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

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

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

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 of c else return -1 then result is the index of the first occurrence of c else result 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

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.

In-class Identify operations for Stack (Pair) 2 minutes

In-class Specify protocols for isEmpty() and pop() (Pair) 5 minutes

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