• No results found

ADTs, Contracts and The Design by Contract Method

N/A
N/A
Protected

Academic year: 2022

Share "ADTs, Contracts and The Design by Contract Method"

Copied!
25
0
0

Loading.... (view fulltext now)

Full text

(1)

CS 617 Object Oriented Systems Lecture 4

ADTs, Contracts and The Design by Contract Method

3:30-5:00pm Mon, Jan 14

Rushikesh K Joshi

Department of Computer Science and Engineering

Indian Institute of Technology Bombay

(2)

Outline

1 From ADT to Classes

2 Specifying Contracts

3 Design by Contract

4 Defensive Programming & Contracts

(3)

Outline

1 From ADT to Classes

2 Specifying Contracts

3 Design by Contract

4 Defensive Programming & Contracts

(4)

From ADTs to classes: Through a Familiar Example

Identify Constructors

Moving to Imperative Version (from applicative specification)

Receiver is modified and not returned

(5)

Converting Stack ADT to interface of class Stack

Stack new (void) →constructor Stack() Stack push (E,Stack) → push (E) E top(Stack) → E(top)

Stack removetop(Stack) → removetop() Boolean empty (Stack) → Boolean empty()

Thus we get:

interface Stack { Stack();

push (E);

E top();

removetop();

Boolean Empty();

}

(6)

Interfaces, Deferred Classes (Abstract Classes) and Concrete Classes

Interfaces: Only the interface functions, cannot be instantiated

Deferred Classes: Partial implementation, cannot be instantiated

Concrete Classes: Fully implemented, can be instantiated

(7)

Our ADT Example

Types:

E is the element type and T be Stack type.

Functions:

T new (void) T push (E,T) E top(T) T removetop(T) Boolean empty (T) Axioms:

empty(new()) top(push(e,t)) = e removetop(push(e,t)) = t not empty(push(e,t)) Preconditions:

.. removetop (T) requires not empty (T)

.. pop (T) requires not empty (T)

(8)

An Abstract Class Specification, Extracting Postconditions

class UnboundedStack { Stack();

precondition: none

postcondition: stack is empty push (E e);

precondition: none

postcondition: (1) top() is e (2) stack not empty E top();

precondition: stack not empty postcondition: no change to stack removetop();

precondition: stack not empty

postcondition: stack has one element less Boolean isEmpty();

}

(9)

Outline

1 From ADT to Classes

2 Specifying Contracts

3 Design by Contract

4 Defensive Programming & Contracts

(10)

Contracts

What are contracts?

How to specify them?

How to use contracts in OO Software Development?

(11)

Contracts

involved between collaborating parties

caller-callee systems: service user and service provider

(12)

Outline

1 From ADT to Classes

2 Specifying Contracts

3 Design by Contract

4 Defensive Programming & Contracts

(13)

The Design by Contract Method of Meyer

(14)

Contract for Member Function Push

(15)

Contract for Member Function Pop (tmp=top,

removetop,return tmp)

(16)

Specifications in Eiffel following Design by Contract I

class Stack [E]

..

top:E is

.. top element require

not empty do

...

end

push(e:E) is

.. add e on top require

not full

(17)

Specifications in Eiffel following Design by Contract II

do ...

ensure

not empty top=e

size=old size+1 end

removetop is

.. removes top element require

not empty do

...

ensure

(18)

Specifications in Eiffel following Design by Contract III

not full

size=old size - 1 end

end

(19)

Preconditions, Postconditions and Invariants

class invariant: a predicate of which the value is true over the entire lifetime of the object

member function precondition: should be satisfied before the execution of the member function

member function postcondition: should be satisfied after the

execution of the member function

(20)

No Redundancy in Implementation

The actual function bodies do not check for preconditions.

Also postconditions are not checked by callers.

(21)

What happens when a contract is violated

An error can be generated, an exception can be thrown.

(22)

Outline

1 From ADT to Classes

2 Specifying Contracts

3 Design by Contract

4 Defensive Programming & Contracts

(23)

Using Assertions

The assert macro in C, C++, assertion support in Java Start with assertions

Add implementations later

1. Design first, then implement

2. Protect implementation against bugs and errors

3. Buggy implementations are caught by the contracts,

assertions

(24)

Preconditions, Postconditions and Inheritance

What happens to them in inheritance?

Can subclasses change them?

(25)

Readings

Bertrand Meyer: Applying Design by Contract, IEEE Computer,

October 1992, pages 40-51.

References

Related documents

Static member function not invoked on object of class Point. int main

• We can re-define the assignment operator for a class/struct by defining the member function operator=. (lhs = rhs) as an assignment expression is

Integrated land-use planning at national and subnational level, carried out in consultation with relevant stakeholders, is another crucial requirement and should include scenario

(i) Life member of Indian Society for Technical Education (ISTE), Delhi (ii) Life member of Indian Water Resources Society (IWRS), Roorkee, India. (iii) Founder Member of

15. On 13 October 2008 CEHRD issued a press statement calling upon the Defendant to mobilise its counter spill personnel to the Bodo creek as a matter of urgency. The

The necessary set of data includes a panel of country-level exports from Sub-Saharan African countries to the United States; a set of macroeconomic variables that would

Percentage of countries with DRR integrated in climate change adaptation frameworks, mechanisms and processes Disaster risk reduction is an integral objective of

Radiological safety and waste management aspects to be considered during the mineral separation of BSM, processing of columbite tantalite ore, processing of rock phosphate and use