Transcript Class 7

cs205: engineering software
university of virginia
Data
Abstraction
David Evans
www.cs.virginia.edu/cs205
fall 2006
Managing Complexity
• Modularity
– Divided problem into procedures
– Used specifications to separate what
from how
• A big program can have thousands of
procedures
cs205: engineering software
2
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?
• Organize program around abstract
data types
cs205: engineering software
3
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
cs205: engineering software
4
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
cs205: engineering software
5
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
6
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()
// EFFECTS: Returns an alive cell state.
static public CellState createDead ()
// EFFECTS: Returns a dead cell state.
public boolean isAlive ()
// EFFECTS: Returns true iff this is alive.
}
cs205: engineering software
7
Cell State Representation
CellState
cs.isAlive ()
clients
Abstract Type
Concrete Representation
class implementation
private boolean alive;
public boolean isAlive () { return alive; }
cs205: engineering software
8
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)
cs205: engineering software
9
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
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 don’t 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 doesn’t use them)
cs205: engineering software
12
Creating Specific Types
public class Set<T> {
...
public boolean add(T el)
public T choose()
public boolean contains(T el)
...
}
Set<String>
cs205: engineering software
public class Set<String> {
...
public boolean add(String el)
public String choose()
public boolean contains(String el)
...
}
13
Abstract Data Type Specifications
• Overview: what the type represents
– Mutability/Immutability
A Set is a mutable, unbounded set of objects
of type T.
– 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
Components of Data Abstractions
• Ways to create objects of the data type
– Creators: create new objects of the ADT
from parameters of other types
– Producers: create new objects of the ADT
from parameters of the ADT type (and
other types)
• Ways to observe properties: observers
• Ways to change properties: mutators
cs205: engineering software
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
cs205: engineering software
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