cs205: engineering software university of virginia fall 2006 data abstraction david evans

34
cs205: engineering software university of virginia fall 2006 Data Abstracti on David Evans www.cs.virginia.edu/cs205

Upload: christal-mccormick

Post on 12-Jan-2016

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

cs205: engineering softwareuniversity of virginia fall 2006

Data Abstraction

David Evanswww.cs.virginia.edu/cs205

Page 2: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

2cs205: engineering software

Managing Complexity

• Modularity– Divided problem into procedures– Used specifications to separate what

from how

• A big program can have thousands of procedures– How can we group them into modules?

Page 3: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

3cs205: engineering software

Abstract Data Types

• Separate what you can do with data from how it is represented

• Client interacts with data through provided operations according to their specifications

• Implementation chooses how to represent data and implement its operations

Page 4: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

4cs205: engineering software

Data Abstraction in Java

• A class defines a new data type• Use private instance variables to

hide the choice of representation– private declarations are only visible

inside the class

Page 5: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

5cs205: engineering software

Up and Down

Abstract Type

Concrete Representation

class implementation

clients

down up

The representation of an abstract data type is visible only in the class implementation.

Clients manipulate an abstract data type by calling its operations (methods and constructors)

Page 6: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

6cs205: engineering software

Cell State Representation

private boolean alive;public boolean isAlive () { return alive; }

Abstract Type

Concrete Representation

class implementation

clients

down up

CellState cs.isAlive ()

Page 7: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

7cs205: engineering software

Advantages/Disadvantages

- More code to write and maintain- Run-time overhead (time to call

method)

+ Client doesn’t need to know about representation

+ Suppose we want to add more states (e.g., question 2)

Page 8: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

8cs205: engineering software

Set Example (ps2)

• Set abstract data type: represent a set of objects

• Operations:– Create an empty set – Mathematical set operations: add,

contains, size, remove, union

Page 9: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

9cs205: engineering software

Type Parameters

• We want to have sets of different types of objects

• How should we declare the Set methods?

public boolean add(?? elem) public boolean contains(?? elem)

public ?? choose()

We don’t want just one Set datatype.We want different Sets for different element types.

Page 10: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

10cs205: engineering software

Generic Datatype

public class Set<T> { ... public boolean add(T el) public T choose() public boolean contains(T el) ...} Note: Java did not support generic

datatypes until version 1.5 (this is why the book doesn’t use them)

Page 11: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

11cs205: engineering software

Creating Specific Typespublic class Set<T> { ... public boolean add(T el) public T choose() public boolean contains(T el) ...} public class Set<String> {

... public boolean add(String el) public String choose() public boolean contains(String el) ...}

Set<String>

Page 12: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

12cs205: engineering software

Abstract Data Type Specifications

• Overview: what the type represents– Mutability/Immutability

A Set is a mutable, unbounded set of objects of type T.

– Abstract NotationA typical Set is { x1, …, xn }.

• Operations: procedural specifications for each operation (public methods and constructors); use the abstract notation introduced in overview.

Page 13: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

13cs205: engineering software

Set Specificationpublic class Set<T> { OVERVIEW: A Set is a mutable, unbounded set of objects of

type T. A typical Set is {x_1, ..., x_n }.

public Set() EFFECTS: Initializes this to an empty set: { }.

public boolean add(T el) MODIFIES: this EFFECTS: Adds el to the elements of this: thispost = thispre U { el } Returns true iff el was not an element of thispre.

Page 14: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

14cs205: engineering software

contains

EFFECTS: Checks if el is an element of this and returns true if it is.

EFFECTS: Returns true iff el is an element of this.

EFFECTS: Returns el this.EFFECTS: Returns el isIn this.

Page 15: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

15cs205: engineering software

union

public void union(Set<T> t)

MODIFIES: thisEFFECTS: Adds the elements of t to this.

MODIFIES: thisEFFECTS: this_post = this_pre t

Specifications should be declarative (what the outcome is), not operational (how it does it).

Page 16: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

16cs205: engineering software

Implementing Abstract Data Types

Page 17: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

17cs205: engineering software

Choosing a Representation• Need a concrete data representation to

store the state– Think about how it maps to abstract state– Think about how methods will be

implemented

• A good representation choice should:– Enable straightforward implementations of

as many methods as possible– Allow performance-critical methods to be

implemented efficiently

Page 18: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

18cs205: engineering software

Set Representation

• Option 1: private T [] rep;– Recall Java arrays are bounded– Easy to implement most methods, hard

to implement insert

• Option 2: private Vector<T> rep;– Easy to implement all methods– Performance may be worse than for

array

Page 19: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

19cs205: engineering software

Implementing Setpublic class Set<T> { // OVERVIEW: Sets are unbounded, mutable sets of elements of type T. // A typical Set is {x1, ..., xn}

// Representation: private Vector rep; public StringSet () { // EFFECTS: Initializes this to be empty: { } rep = new Vector (); } public void insert (String s) { // MODIFIES: this // EFFECTS: Adds s to the elements of this: // this_post = this_pre U { s } rep.add (s); }

Could this implementation of insert be correct?

Page 20: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

20cs205: engineering software

It depends…

public int size () { // EFFECTS: Returns the number of elements in this. Set<T> unique = new Set<T> (); for (T el : rep) { if (!unique.isIn (el)) { unique.add (current); } } return unique.rep.size (); }

Page 21: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

21cs205: engineering software

Is it correct?

public int size () { // EFFECTS: Returns the number of // elements in this. return rep.size (); }

public void insert (String s) { if (!contains (s)) rep.add (s);}

Page 22: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

22cs205: engineering software

Reasoning About Data Abstractions

• How can we possibly implement data abstractions correctly if correctness of one method depends on how other methods are implemented?

• How can we possibly test a data abstraction implementation if there are complex interdependencies between methods?

Page 23: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

23cs205: engineering software

What must we know to know if size is correct?

This implementation is correct only if we know the rep does not contain duplicates

public int size () { // EFFECTS: Returns the number of // elements in this. return rep.size (); }

Page 24: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

24cs205: engineering software

To Reason about Operations

• We need to know:• How the concrete rep maps to

abstract values: Abstraction Function

• What values of the concrete rep are valid: Representation Invariant

Page 25: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

25cs205: engineering software

Rep Invariant

• Predicate that all legitimate objects of the ADT must satisfy

I: C → boolean• Helps us reason about correctness of

methods independently– Prove all objects satisfy the invariant

before leaving the implementation code– Assume all objects passed in satisfy the

invariant

Page 26: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

26cs205: engineering software

Reasoning with Rep Invariants

REQUIRES: Rep Invariant is true for this (and

any other reachable ADT objects)

EFFECTS: Rep Invariant is true for all new and modified ADT objects on exit.

Every public datatype operation implicitly includes these preconditions and postconditions.

Page 27: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

27cs205: engineering software

Rep Invariant for Setpublic class Set { // Representation: private Vector<T> rep;

// RepInvariant (c) = c contains no duplicates

or // RepInvariant (c) = // forall i, j: rep[i].equals(rep[j]) // only when i == j

Page 28: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

28cs205: engineering software

Implementing Insert?

public void insert (String s) { // MODIFIES: this // EFFECTS: Adds s to the elements of this: // this_post = this_pre U { s } rep.add (s); }

Not a correct implementation: after it returns this might not satisfy the rep invariant!

Page 29: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

29cs205: engineering software

Implementing Insert

public void insert (String s) { // MODIFIES: this // EFFECTS: Adds s to the elements of this: // this_post = this_pre U { s } if (!contains (s)) { rep.add (s); } }Possibly correct implementation: we need to know how to map rep to abstraction notation to know if

this_post = this_pre U { s }

Page 30: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

30cs205: engineering software

Abstraction Function• The Abstraction Function maps a

concrete state to an abstract state:AF: C → A

Function from concrete representation to the abstract notation introduced in overview specification.

What is the range of the Abstraction Function?

Range is concrete states for which rep invariant is true

Page 31: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

31cs205: engineering software

Abstraction Function for Set

public class Set<T> {// OVERVIEW: Sets are unbounded, // mutable sets of objects of type T.

// A typical Set is {x1, ..., xn}

// Representation: private Vector<T> rep;

// AF (c) = // { AFT (c.rep.elementAt(i)) | 0 <= i < c.rep.size () }

Page 32: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

32cs205: engineering software

Correctness of Insert

public void insert (String s) { // MODIFIES: this // EFFECTS: Adds s to the elements of this: // this_post = this_pre U { s } if (!contains (s)) { rep.add (s); } }

Use abstraction function to show if add implements its specification, then AF(rep_post) = AF(rep_pre) U {AFString(s)}

Page 33: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

33cs205: engineering software

Reality Check• Writing abstraction functions, rep invariants,

testing code thoroughly, reasoning about correctness, etc. for a big program is a ridiculous amount of work!

• Does anyone really do this?– Yes (and a lot more), but usually only when its

really important to get things right:

• Cost per line of code:– Small, unimportant projects: $1-5/line– WindowsNT: about $100/line– FAA’s Automation System (1982-1994): $900/line

Page 34: Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

34cs205: engineering software

Charge• PS3: out today, due next Monday

– Reason about data types using abstraction functions and rep invariants

– Implement the DirectedGraph abstract data type you used in PS2

• Wednesday: Quiz 2– Mostly on data abstraction– Chapter 5 and lectures– Maybe a question or two on testing