Professional Documents
Culture Documents
Data Abstraction
www.cs.virginia.edu/cs205
David Evans
Managing Complexity
Modularity
Divided problem into procedures Used specifications to separate what from how
Data Abstraction
We need new data types, not just procedures
Could you implement PS2 without the DirectedGraph type? We could make procedures, but what would you pass to them?
Up and Down
Clients manipulate an abstract data type by calling its operations (methods and constructors)
clients
Abstract Type Concrete Representation
class implementation The representation of an abstract data type is visible only in the class implementation.
cs205: engineering software
Example: CellState
public class CellState {
// OVERVIEW: A CellState is an immutable // object that represents the state of a cell, // either alive or dead.
static public CellState createAlive()
class implementation
Advantages/Disadvantages
- More code to write and maintain - Run-time overhead (time to call method) + Client doesnt need to know about representation + Suppose we want to add more states (e.g., question 2)
cs205: engineering software
10
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 dont want just one Set datatype. We want different Sets for different element types.
cs205: engineering software
11
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 doesnt use them)
12
Set<String>
public class Set<String> { ... public boolean add(String el) public String choose() public boolean contains(String el) ... }
13
Abstract Notation
A typical Set is { x1, , xn }.
Operations: procedural specifications for each operation (public methods and constructors); use the abstract notation introduced in overview.
cs205: engineering software
14
Set Specification
public class Set<T> implements Iterable<T>, Collection<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.
cs205: engineering software
15
16
Set Operations
public class Set<T> implements Iterable<T>, Collection<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 Set(Set<T> s) EFFECTS: Initializes this to a set containing the same elements as the set s (a shallow copy).
public T choose() REQUIRES: this has at least one element EFFECTS: Returns an element of this.
public boolean contains(Object el) EFFECTS: Returns true iff el is an element of this.
public int size() public boolean add(T el) EFFECTS: Returns the number of elements in this. MODIFIES: this EFFECTS: Adds el to the elements of this: public boolean isEmpty() this_post = this_pre U { el } EFFECTS: Returns true iff this has no elements. Returns true iff el was not an element of this_pre.
public void union(Set<T> t) MODIFIES: this EFFECTS: this_post = this_pre U t public boolean remove(Object el) MODIFIES: this EFFECTS: Removes el from this: this_post = this_pre - { el } Returns true iff el is in this_pre
17
PS2
Several datatype implementations provided with specifications Client interacts with data type using the methods as described in the specification Client does not know the concrete representation Your code should work correctly with any correct implementation of the specified datatype
cs205: engineering software
18